模型之间有以下关系:
class Gift
has_and_belongs_to_many :constituents, :autosave => true
accepts_nested_attributes_for :constituents
end
class Constituent
has_and_belongs_to_many :gifts
has_many :addresses, :autosave => true
end
class Address
belongs_to :constituent
end
只有在@gift.save
记录中检测到更改时,我的控制器address
才会更新相应的constituent
记录。
但如果address
记录发生变化但constituent
记录中没有变化,则address
记录不会更新。
在我的控制器中,我只是运行save!
方法:
def create
@gift = Gift.new
@gift.assign_attributes gift_params
p @gift.constituents[0].addresses[0].changed? #Prints true
@gift.save!
end
def gift_params
params.require(:gift).permit(:amount, :constituents_attributes => [:id, :organization_name, :title_id, :first_name, :last_name, :addresses_attributes => [ :id, :address_type, :address1, :address2, :city, :country_id, :state_id, :pin_code, :constituent_id ]])
end
参数的一个例子是:
{"utf8"=>"✓", "gift"=>
{"constituents_attributes"=>{"0"=>{"id"=>"5", "title_id"=>"", "first_name"=>"Mridula", "last_name"=>"Prabhu",
"addresses_attributes"=>{"0"=>{"id"=>"16", "constituent_id"=>"5", "address_type"=>"home", "address1"=>"Line1", "address2"=>"Line2", "city"=>"Pune", "country_id"=>"38", "state_id"=>"53", "pin_code"=>"M2M 2M2"
}
},
"amount"=>"20"
}
我该如何解决这个问题?