加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 覆盖rails中的嵌套属性

发布时间:2020-12-16 19:40:13 所属栏目:百科 来源:网络整理
导读:当我在rails中覆盖嵌套属性方法时会发生什么.例如, class Order has_many :line_items accepts_nested_attributes_for :line_items def line_item_attributes=(attr) # what can I do here. endendclass LineItem belongs_to :orderend 在上面的代码中, 里面
当我在rails中覆盖嵌套属性方法时会发生什么.例如,
class Order
  has_many :line_items
  accepts_nested_attributes_for :line_items

  def line_item_attributes=(attr)
   # what can I do here.
  end
end

class LineItem
  belongs_to :order
end

在上面的代码中,

>里面的line_item_attributes =方法,我可以添加/修改/删除订单的行项吗?
>什么时候是line_items_attributes =被调用,如果我调用@ order.save(params)?

解决方法

>是的,你可以只要打电话

assign_nested_attributes_for_collection_association(:line_items,attributes,mass_assignment_options)

当你完成了

检查源:#文件activerecord / lib / active_record / nested_attributes.rb,第263行

>从文档:

Saving

All changes to models,including the destruction of those marked for
destruction,are saved and destroyed automatically and atomically when
the parent model is saved. This happens inside the transaction
initiated by the parents save method. See
ActiveRecord::AutosaveAssociation.

我不认为覆盖这种方法是个好主意.我将你的代码添加到after_save钩子.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读