views:

201

answers:

2

Hi all,

I'm having trouble getting my InventoryItem to accept nested attributes which is strange.

In my script/console, I did the following:

>> InventoryItem.create!(:name => 'what', :image_attributes => [ {:image => File.open("/home/davidc/Desktop/letterbx.jpg", "r") }])
ActiveRecord::UnknownAttributeError: unknown attribute: image_attributes

I am not sure why I'm getting the unknown attribute error when in my model, I already did accept_nested_attributes.

I'm using Rails v2.3.5.

Inventory Item Model

class InventoryItem < ActiveRecord::Base
  uuid_it

  belongs_to :user
  has_many :orders
  has_many :images, :validate => true
  accepts_nested_attributes_for :images
end

Image

class Image < ActiveRecord::Base
  belongs_to :inventory_item

  has_attached_file :image, :style => { :medium => "300x300>", :thumb => "100x100>" }
end
A: 

:image_attributes is supposed to be a hash.

InventoryItem.create!(
   :name => 'what',
   :image_attributes => { ... }
)
j.
A: 

You have has_many :images So, it should be :images_attributes, not :image_attributes

InventoryItem.create!(:name => 'what', :images_attributes => [ {:image => File.open("/home/davidc/Desktop/letterbx.jpg", "r") }])

And it is correct to use array of hashes when you have has_many relationship

Voyta