Forum: Ruby on Rails ActiveRecord Error with << in Collection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dcbf676f860477e44b275cae5d6318a4?d=identicon&s=25 Piotr Usewicz (lopmx)
on 2005-12-22 00:27
(Received via mailing list)
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
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-22 00:42
(Received via mailing list)
On Dec 21, 2005, at 3:26 PM, Piotr Usewicz 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
Dcbf676f860477e44b275cae5d6318a4?d=identicon&s=25 Piotr Usewicz (lopmx)
on 2005-12-22 03:12
(Received via mailing list)
>
>                                         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...
:/
This topic is locked and can not be replied to.