Tuesday, 10 September 2013

Creating nested association records Ruby on Rails

Creating nested association records Ruby on Rails

Apparently I am overlooking something very simple in the example I have
done below where I am attempting to create nested association records
while instantiating a new parent record.
I'm hoping a fresh set of eyes my help find what has been eating at me for
days. Thanks in advance! What am I missing/messing up? It seems so
trivial.
ActiveRecord::NestedAttributes is obviously unhappy.
class ContentGroup < ActiveRecord::Base
attr_protected
has_many :contents, :dependent=>:destroy
accepts_nested_attributes_for :contents
end



class Content < ActiveRecord::Base
attr_protected
has_one :sort_item, :as=>:sortable
belongs_to :content_group, :dependent=>:destroy
accepts_nested_attributes_for :sort_item
end



class SortItem < ActiveRecord::Base
attr_accessible :position, :sortable_id, :sortable_type
belongs_to :sortable, :polymorphic=>true
end



in rails console, lightly nested call works as expected:
> p = {"sort_item_attributes"=>{"position"=>"1"}}
> b = Content.new(p)
=> Content id: nil, content_group_id: nil
one additional nest blows up:
> p = {"contents_attributes"=>{"sort_item_attributes"=>{"position"=>"1"}}}
> cg = ContentGroup.new(p)
ActiveRecord::UnknownAttributeError: unknown attribute: position from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:88:in
block in assign_attributes' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:ineach'
from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:78:in
assign_attributes' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/base.rb:498:ininitialize'
from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/reflection.rb:183:in
new' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/reflection.rb:183:inbuild_association'
from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:233:in
build_record' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:112:inbuild'
from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:406:in
block in assign_nested_attributes_for_collection_association' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:401:ineach'
from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:401:in
assign_nested_attributes_for_collection_association' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/nested_attributes.rb:289:incontents_attributes='
from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:94:in
block in assign_attributes' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:93:ineach'
from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/attribute_assignment.rb:93:in
assign_attributes' from
/.rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.13/lib/active_record/base.rb:498:ininitialize'
from (irb):10:in new' from (irb):10 from
/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands/console.rb:47:instart'
from
/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in
start' from
/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:41:in'
from script/rails:6:in `require'
Hope this can be fixed, thanks for the awesome gem!

No comments:

Post a Comment