Andrew Kaspick
2011-Nov-23 20:59 UTC
associations and nested_attributes internals... looking for patch guidance
Hello, I ran into something that works in Rails 3.0.x, but no longer works in Rails 3.1.x and it seems to be valid usage and I was wondering if somebody more familiar with the activerecord internals could point me in the right direction... Here''s my basic use case using nested attributes: project = Project.find 1 params[:task] = {:name => ''task'', :project_attributes => {''1'' => {:name => ''project''}}} # this works in rails 3.0.11 (the project name is assigned correclty) # this does not work in rails 3.1.3 project.tasks.new params[:task] # this works in rails 3.1.3 # even though we''re creating the new task from the relation owned by the project object, we need to pass the project id in explicitly so that it updates correctly project.tasks.new params[:task].merge(:project_id => project.id) There has been a lot of refactoring of active record between Rails 3.0 and 3.1, so I''m a bit lost in the current code, but I''ve found the main culprit to be in nested_attributes.rb https://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L327 The line with: record = send(association_name) ... that correctly returns the project record in Rails 3.0, but it''s nil in 3.1. I''ve traced (I think) the origination of this issue in Rails 3.1 to https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/association.rb#L233 When building the association (the "owner" value in this scope has the valid project) we lose reference to the project owner value which we would need to successfully assign the nested attributes. I''ll provide a patch for this issue, but I feel I''m missing something simple here on how to assign the owner of that association before it tries to assign the nested attributes. If somebody can point me in the right direction, I''ll work on a patch. Thanks -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Andrew Kaspick
2011-Dec-15 05:38 UTC
Re: associations and nested_attributes internals... looking for patch guidance
3 weeks later (got busy), here''s my patch... https://github.com/rails/rails/pull/3991 On Wed, Nov 23, 2011 at 10:59 AM, Andrew Kaspick <akaspick@gmail.com> wrote:> Hello, > > I ran into something that works in Rails 3.0.x, but no longer works in > Rails 3.1.x and it seems to be valid usage and I was wondering if > somebody more familiar with the activerecord internals could point me > in the right direction... > > Here''s my basic use case using nested attributes: > > project = Project.find 1 > params[:task] = {:name => ''task'', :project_attributes => {''1'' => > {:name => ''project''}}} > > # this works in rails 3.0.11 (the project name is assigned correclty) > # this does not work in rails 3.1.3 > project.tasks.new params[:task] > > # this works in rails 3.1.3 > # even though we''re creating the new task from the relation owned by > the project object, we need to pass the project id in explicitly so > that it updates correctly > project.tasks.new params[:task].merge(:project_id => project.id) > > There has been a lot of refactoring of active record between Rails 3.0 > and 3.1, so I''m a bit lost in the current code, but I''ve found the > main culprit to be in nested_attributes.rb > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L327 > > The line with: > record = send(association_name) > > ... that correctly returns the project record in Rails 3.0, but it''s nil in 3.1. > > I''ve traced (I think) the origination of this issue in Rails 3.1 to > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/association.rb#L233 > When building the association (the "owner" value in this scope has the > valid project) we lose reference to the project owner value which we > would need to successfully assign the nested attributes. > > I''ll provide a patch for this issue, but I feel I''m missing something > simple here on how to assign the owner of that association before it > tries to assign the nested attributes. If somebody can point me in > the right direction, I''ll work on a patch. > > Thanks-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.