ActiveRecord Error with << in Collection


#1

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:inreplace’
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:inreplace’
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:inattributes=’
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:ininitialize’
#{RAILS_ROOT}/app/controllers/cmdb_controller.rb:24:in new' #{RAILS_ROOT}/app/controllers/cmdb_controller.rb:24:increate’

=============== 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


#2

On Dec 21, 2005, at 3:26 PM, Piotr U. wrote:

active_record/associations.rb:770:in `attributes=’

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.attribute_id= attr_id
  	render :action => 'new'

ConfigurationItem: has_many :attributes, :class_name =>
‘ConfigurationItemAttributeValue’

ConfigurationItemAttributeValue: belongs_to :configuration_item


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#3
                                    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

Yeah 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…
:confused: