Code Challenge - Override Nested Attributes Call Method

389 views
Skip to first unread message

Linda

unread,
Jun 15, 2011, 4:22:27 PM6/15/11
to Philly.rb
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

Mat Schaffer

unread,
Jun 15, 2011, 11:07:35 PM6/15/11
to phil...@googlegroups.com
The best option I was able to google is doing the data manipulations as part of a reject_if block:


But I'm wondering if maybe this is a case to roll your own version of accepted attribute handling and call that from the controller instead of Announcement.new. Seems like that might be a little less hacky than the reject_if overloading.

-Mat


--
You received this message because you are subscribed to the Google Groups "Philly.rb" group.
To post to this group, send email to phil...@googlegroups.com.
To unsubscribe from this group, send email to phillyrb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/phillyrb?hl=en.


Linda Cao

unread,
Jun 16, 2011, 7:25:06 AM6/16/11
to phil...@googlegroups.com
Good Morning Matt, 
Many thanks for your kind help as always.  I mimicked the reject_if approach and am working to make the syntax right (theirs is one vs. mine many in nested relationship).   Will try out your suggested approach in the controller.  The nested attributes are useful except conflicting when saving to existing record (need to override first).  Rails 3.1 will allow snow chaining many to many :through via nested attributes.  

Happy Summer and Cheers,
Linda

Mat Schaffer

unread,
Jun 16, 2011, 10:01:58 AM6/16/11
to phil...@googlegroups.com
Oh cool, didn't realize 3.1 had nested attributes upgrades in it. It's one Rails feature that always felt overly complicated. Glad to hear it's getting some attention.
-Mat

Linda Cao

unread,
Jun 16, 2011, 11:32:56 AM6/16/11
to phil...@googlegroups.com
Hi Matt, completely agree - nested attributes is nasty to touch under restrictions. I don't know other alternatives if one needs to put attributes from two models into one form and save the record.

Ryan did a nice overview about Rails 3.1 http://railscasts.com/episodes/265-rails-3-1-overview

Cheers,
Linda
Reply all
Reply to author
Forward
0 new messages