2017-08-27 83 views
0

我在Rails開發初學者,我目前在市場上的改進工作已經實現了,我被卡住。內容複製到另一個表的屬性在軌

我想修改代碼,以便它在其中被命名爲「列表」另一個表的另一個屬性名爲「custom_field_values」的表的屬性的複製內容。

我在custom_field_value模型寫了這個代碼:

before_validation :set_date_value 
    def set_date_value 
    @current_listing = Listing.where(id: listing_id).select(:valid_until) 
    @current_listing= date_value 
    end 

出現了一些問題,因爲表值接收空,而不是內容本身。 我也試着使用listing_controller來加入這兩個表並分配值但沒有任何反應,我仍然得到一個空值。

這裏的方式是兩個表模式:

表名:custom_field_values

id    :integer   not null, primary key 
    custom_field_id :integer 
    listing_id  :integer 
    text_value  :text(65535) 
    numeric_value :float(24) 
    date_value  :datetime 
    created_at  :datetime   not null 
    updated_at  :datetime   not null 
    type   :string(255) 

表名:上市

id        :integer   not null, primary key 
    uuid       :binary(16)  not null 
    community_id     :integer   not null 
    author_id      :string(255) 
    category_old     :string(255) 
    title       :string(255) 
    times_viewed     :integer   default(0) 
    language      :string(255) 
    created_at      :datetime 
    updates_email_at    :datetime 
    updated_at      :datetime 
    last_modified     :datetime 
    sort_date      :datetime 
    listing_type_old    :string(255) 
    description      :text(65535) 
    origin       :string(255) 
    destination      :string(255) 
    valid_until      :datetime 
    delta       :boolean   default(TRUE), not null 
    open       :boolean   default(TRUE) 
    share_type_old     :string(255) 
    privacy       :string(255)  default("private") 
    comments_count     :integer   default(0) 
    subcategory_old     :string(255) 
    old_category_id     :integer 
    category_id      :integer 
    share_type_id     :integer 
    listing_shape_id    :integer 
    transaction_process_id   :integer 
    shape_name_tr_key    :string(255) 
    action_button_tr_key   :string(255) 
    price_cents      :integer 
    currency      :string(255) 
    quantity      :string(255) 
    unit_type      :string(32) 
    quantity_selector    :string(32) 
    unit_tr_key      :string(64) 
    unit_selector_tr_key   :string(64) 
    deleted       :boolean   default(FALSE) 
    require_shipping_address  :boolean   default(FALSE) 
    pickup_enabled     :boolean   default(FALSE) 
    shipping_price_cents   :integer 
    shipping_price_additional_cents :integer 
    availability     :string(32)  default("none") 
+0

我明白你的問題。但是,您能否公開更多模型代碼,以便我可以給您最好的答案? 現在,你可以寫 '@current_listing = Listing.where(ID:listing_id)。選擇(:valid_until)' '@current_listing.date_value = date_value' '@ current_listing.save' –

回答

0

這裏是校正,使用發現,而不是在那裏找到一個記錄,其中將返回記錄集,並可以訪問模型採用自內列字段

before_validation :set_date_value 
def set_date_value 
    @current_listing = Listing.find_by(id: self.listing_id) 
    # if find successfully (not nil) 
    if @current_listing 
    @current_listing.valid_until = self.date_value 
    end 
end 
+1

如果沒有發現上市通過'Listing.find',代碼將拋出一個ActiveRecord :: RecordNotFound錯誤。所以不用檢查'if @ current_listing'。你可以做'Listing.find_by(id:self.listing_id)'然後檢查@current_listing。 –

+0

感謝,與.find_by_id – widjajayd

+0

find_by_id工作正常,但有利於更靈活的語法,就像find_by是被反對的修正(ID:SOME_ID)。乾杯。 –

0

我的解決方案是,您應該使用列表關聯而不是單獨查詢Listing,並使用實例變量來保存它。

def set_date_value 
    listing.valid_until = date_value if listing 
end 

這將使用belongs_to :listing關聯設置valid_until列,更簡潔。

+0

感謝您使用這個解決方案,但是我使用了它,並且在valid_date中獲得了空值。我嘗試將另一個custom_field_value模型的靜態值傳遞給列表表,仍然是Null值。 –

+0

如果你得到零,模型中肯定有一些問題。可以提供更多信息,也許會顯示更多的模型代碼。你如何獲得價值? –

+0

我可以通過一個短暫的Skype會話幫助您 –

相關問題