David Secret
2010-May-18 22:01 UTC
ActiveRecord / Nested Attributes create missing association
Hi I''m working on an issue described in this ticket: https://rails.lighthouseapp.com/projects/8994/tickets/2415-accepts_nested_attributes_for-doest-work-when-nested_attributes-hash-has-an-id-entry#ticket-2415-6 I''ve a nested form, in which the user can either create a new entry or choose an existing one. Choosing an existing entry in a nested form is a problem, because the association in the join table won''t be created (in Rails 2.3.6 the ActiveRecord::RecordNotFound exception will be raised as described in the ticket). I had a look at the patch in the ticket and amended it, so when the id in nested attributes is set, I read the record from the db and create the association in the join table. Unfortunately, creating the association is not working properly. here''s my code: reflection self.class.reflect_on_association(association_name) # fetch the record record eval("#{reflection.klass.name}.find(#{attributes[''id'']})") if record.nil? raise_nested_attributes_record_not_found(association_name, attributes[''id'']) end # create the association association.send(:add_record_to_target_with_callbacks, record) unless association.loaded? assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) The last two lines doesn''t create the association. I looked at the active record code but haven''t found a way to create the association only. All the build/create/update methods will create a new record. Would be nice if someone who''s familiar with the active record code could help me out! Let me know if you need more information on this. Thanks, David -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Bln
2010-May-19 12:47 UTC
Re: ActiveRecord / Nested Attributes create missing associat
one more info: the code above works when I create a new parent (and either create a new child or choose an existing one in the nested form). The code doesn''t work when I edit the model and choose an existing child record (the association won''t be created), any hints? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.