【问题标题】:Copying content of one table's attribute to another table's attribute in rails将一个表属性的内容复制到rails中另一个表的属性
【发布时间】: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


【解决方案1】:

我的解决方案是您应该使用列表关联而不是单独查询Listing 并使用实例变量来保存它。

def set_date_value
  listing.valid_until = date_value if listing
end

这将使用belongs_to :listing 关联来设置valid_until 列并且更加简洁。

【讨论】:

  • 感谢您提供此解决方案,但我使用了它,并且在 valid_date 中获得了 Null 值。我试图将另一个 custom_field_value 模型中的静态值传递给 Listings 表,但仍然是 Null 值。
  • 如果你得到 Nil,你的模型肯定有问题。可以提供更多信息,可能会显示更多您的模型代码。您如何访问该值?
  • 我可以通过简短的 Skype 会话帮助您
  • 我们能否进行一次视频群聊而不是 Skype 这是我的电子邮件地址:yasserkotrsi@gmail.com 非常感谢。
【解决方案2】:

这里是更正,使用 find 而不是 where 找到一条记录,where 将返回记录集合,并访问模型内​​的列字段使用 self

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

【讨论】:

  • 如果没有找到 Listing.find 的列表,代码将抛出 ActiveRecord::RecordNotFound 错误。所以没有用检查if @current_listing。你可以做Listing.find_by(id: self.listing_id) 然后检查@current_listing。
  • find_by_id 工作正常,但不推荐使用更灵活的语法,例如 find_by(id: some_id)。干杯。
  • 感谢您提供此解决方案,但我使用了它,并且在 valid_date 中获得了 Null 值。我尝试将另一个 custom_field_value 模型中的静态值传递给列表表,但仍然是 Null 值。
  • 如果把 before_validation 改成 before_save 怎么样
  • @widjajayd 我将其更改为 before_save 但它不会更改输出。
猜你喜欢
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多