Hi
I get a following error:
ActiveRecord::AssociationTypeMismatch in Cmdb#create
ConfigurationItemAttributeValue expected, got Array
RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:128:in
`raise_on_type_mismatch''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`replace''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`each''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`replace''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:770:in
`attributes=''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:762:in
`attributes=''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1188:in
`initialize_without_callbacks''
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:236:in
`initialize''
#{RAILS_ROOT}/app/controllers/cmdb_controller.rb:24:in `new''
#{RAILS_ROOT}/app/controllers/cmdb_controller.rb:24:in `create''
=============== Controller ================
def create
@ci = ConfigurationItem.new(params[:ci])
if(params[:attribute_id])
params[:attribute_id].each_pair{ |attr_id, |attr_value_id|
attribute = ConfigurationItemAttributeValue.new
attribute.attribute_id= attr_id
attribute.value_id= attr_value_id
@ci.attributes << attribute
}
end
if(params[:attribute_value])
params[:attribute_value].each_pair{ |attr_id, |attr_value|
attribute = ConfigurationItemAttributeValue.new
attribute.attribute_id= attr_id
attribute.value= attr_value
@ci.attributes << attribute
}
end
if @ci.save
flash[:notice] = "Configuration Item `#{@ci.name}` was successfully
created."
redirect_to :action => ''list''
else
render :action => ''new''
end
end
==============
The line that should work is @ci.attributes << attribute
attribute is a ConfigurationItemAttributeValue.new, so it is NOT an
array. What am I doing wrong? (the associations are set correctly, I
suppose)
ConfigurationItem: has_many :attributes, :class_name =>
''ConfigurationItemAttributeValue''
ConfigurationItemAttributeValue: belongs_to :configuration_item
On Dec 21, 2005, at 3:26 PM, Piotr Usewicz wrote:> Hi > > I get a following error: > > ActiveRecord::AssociationTypeMismatch in Cmdb#create > > ConfigurationItemAttributeValue expected, got Array > > RAILS_ROOT: ./script/../config/.. > Application Trace | Framework Trace | Full Trace > > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations/association_collection.rb:128:in > `raise_on_type_mismatch'' > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations/association_collection.rb:114:in `replace'' > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations/association_collection.rb:114:in `each'' > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations/association_collection.rb:114:in `replace'' > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations.rb:770:in `attributes='' > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations.rb:762:in `attributes='' > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/base.rb:1188:in `initialize_without_callbacks'' > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/callbacks.rb:236:in `initialize'' > #{RAILS_ROOT}/app/controllers/cmdb_controller.rb:24:in `new'' > #{RAILS_ROOT}/app/controllers/cmdb_controller.rb:24:in `create'' > > =============== Controller ================> > def create > @ci = ConfigurationItem.new(params[:ci]) > > if(params[:attribute_id]) > params[:attribute_id].each_pair{ |attr_id, |attr_value_id|What''s up with your block here. ^^^^^^ |attr_id, |attr_value_id| should be |attr_id, attr_value_id| it looks like you are adding an extra | or pipe char to your block variables there. -Ezra> attribute = ConfigurationItemAttributeValue.new > attribute.attribute_id= attr_id > attribute.value_id= attr_value_id > @ci.attributes << attribute > } > end > > if(params[:attribute_value]) > params[:attribute_value].each_pair{ |attr_id, |attr_value| > attribute = ConfigurationItemAttributeValue.new > attribute.attribute_id= attr_id > attribute.value= attr_value > @ci.attributes << attribute > } > end > > if @ci.save > flash[:notice] = "Configuration Item `#{@ci.name}` was > successfully created." > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > ==============> > The line that should work is @ci.attributes << attribute > > attribute is a ConfigurationItemAttributeValue.new, so it is NOT an > array. What am I doing wrong? (the associations are set correctly, > I suppose) > > ConfigurationItem: has_many :attributes, :class_name => > ''ConfigurationItemAttributeValue'' > > ConfigurationItemAttributeValue: belongs_to :configuration_item > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
> > What''s up with your block here. > ^^^^^^ > |attr_id, |attr_value_id| should be |attr_id, > attr_value_id| > > it looks like you are adding an extra | or pipe char to your block > variables there. > > -EzraYeah thanks, fixed it, but it still does not solve the problem. It does generate the error even if i push an empty object to the collection... :/
Apparently Analagous Threads
- Dynamic # of Attributes for a Model (key/val table) Get/Set?
- Use repadmin /showobjmeta would caused samba 4.3.0 fault
- join tables and position (acts_as_list)
- active record wants root password, not sure why ..
- Repadmin fails when querying Samba server 4.7.6