Hi All, can you advise how to override nested attributes call method
'self.recipients_attributes=' when the recipient's email is already in
the database? I tried
assign_nested_attributes_for_collection_association(:recipients,
attributes) + attributes.stringify_keys! + dynamic finder to overcome,
however it did not work. Recipient email is specified to be unique in
the model. Thanks in advance, Linda
class Announcement < ActiveRecord::Base
attr_accessible :content, :recipients_attributes
belongs_to :user
has_many :recipients, :through => :awards, :dependent => :destroy
has_many :awards, :dependent => :destroy
accepts_nested_attributes_for :recipients, :reject_if => lambda { |
a| a[:email].blank? }, :allow_destroy => true
def recipients_attributes=(attributes = nil)
attributes.stringify_keys!
attributes.each do |key, recipient|
if recipient = recipients.find_by_email(recipient['email'])
attributes.delete(key)
end
end
assign_nested_attributes_for_collection_association(:recipients,
attributes)
end
end
detailed code
http://pastie.org/2074124