【发布时间】:2018-02-04 21:45:03
【问题描述】:
我是 Rails 开发的初学者,目前正在对已经实施的市场进行增强,但我陷入了困境。
我想修改代码,以便将名为“custom_field_values”的表属性的内容复制到另一个名为“listings”的表的另一个属性中。
我在 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
但是出了点问题,因为表值接收 Null 而不是内容本身。 我还尝试使用 listing_controller 连接两个表并分配值,但没有任何反应,我仍然得到一个 Null 值。
顺便说一下,这里有两个表模式:
表名: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")
【问题讨论】:
-
我了解您的问题。但是您能否公开更多模型的代码,以便我给您最好的答案?现在,你可以写
@current_listing = Listing.where(id: listing_id).select(:valid_until)@current_listing.date_value = date_value@current_listing.save
标签: mysql ruby-on-rails activerecord model-view-controller