【发布时间】:2014-03-08 21:10:42
【问题描述】:
我正在尝试在将嵌套属性保存到我的数据库之前对其进行修改。这个想法是,如果有人已经在服装数据库中提交了带有给定cid 的服装,我们应该提取那个并将其用于当前协议。但是,通过调试,我发现嵌套的属性数组根本没有改变。有什么想法吗?
谢谢! 马特
app/models/agreement.rb
class Agreement < ActiveRecord::Base
before_save :get_costumes
has_and_belongs_to_many :costumes, join_table: :agreement_costumes
accepts_nested_attributes_for :costumes, reject_if: proc { |attributes| attributes['cid'].blank? }
has_many :drycleans
accepts_nested_attributes_for :drycleans, allow_destroy: true, reject_if: :all_blank
def get_costumes
self.costumes.map! { |costume|
unless Costume.where(cid: costume.cid).nil?
Costume.where(cid: costume.cid).first
end
}
end
end
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-4 nested-attributes