Forum: Ruby on Rails Nested hash from form params into create()?

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.
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-02-27 21:12
(Received via mailing list)
Hi,

I'm really excited about getting the create() method working on a
nested hash. The Rails book make it seem like I'm close to doing the
right thing but I think something very small is not quite right. The
error at the below isn't giving any hints that I understand.

I have a form that produces a nested params hash and sends it to my
add_to_cart controller method. Params contains a product variation_id
and a list of chosen options (eg. color, size, etc).

Any help greatly appreciated.

Thanks,
Peter



params =
{
  "commit"=>"Add To Cart",
  "variation_id"=>"1",
  "cart_item_options"=>{"6"=>{"option_id"=>"74"},
	                                   "7"=>{"option_id"=>"97"},
			                   "5"=>{"option_id"=>"47"}}
}

## CONTROLLER ##

def add_to_cart
  CartItem.create(params}
end


## MODELS ##

class CartItem < ActiveRecord::Base
  belongs_to :variation
  has_many :cart_item_options
end

class CartItemOption < ActiveRecord::Base
  belongs_to :cart_item
end


## TABLES ##

CREATE TABLE cart_items (
  id SERIAL,
  variation_id INTEGER UNSIGNED NOT NULL
);

CREATE TABLE cart_item_options (
  id SERIAL,
  cart_item_id INTEGER UNSIGNED NOT NULL,
  optional_id INTEGER UNSIGNED NOT NULL
);


## ERROR ##

CartItemOptionalVariation expected, got Array

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:128:in
`raise_on_type_mismatch'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`replace'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/association_collection.rb:114:in
`replace'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:770:in
`cart_item_optional_variations='
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:762:in
`cart_item_optional_variations='
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in
`attributes='
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in
`each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in
`attributes='
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1188:in
`initialize_without_callbacks'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:236:in
`initialize'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in
`new'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in
`create'
./script/../config/../app/controllers/store_controller.rb:79:in
`add_to_cart'
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-02-27 22:06
(Received via mailing list)
Sorry, I posted an error from a bigger version of the problem. Here is
the smaller version's error message. It seems like create() doesn't
like having any extra stuff sent to it. However, I don't think I
should have to remove commit from the params hash for it to work as
nicely as the rails book implies.

Thanks,
Peter


undefined method `commit=' for #<CartItem:0x27239bc>

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1498:in
`method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1337:in
`attributes='
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in
`each'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1336:in
`attributes='
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1188:in
`initialize_without_callbacks'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:236:in
`initialize'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in
`new'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:449:in
`create'
./script/../config/../app/controllers/store_controller.rb:79:in
`add_to_cart'
This topic is locked and can not be replied to.