I’m trying to create a simple merch association between two Products. I’ve followed a few tutorials and think I have it right, but it keep throwing an error whenever I try to add an associated item directly. I’m new to ROR, could someone help me understand what I’ve done wrong here?
class Product < ActiveRecord::Base
attr_accessible :assoc_product,:product_id, :merch_associations, :aux_description, :buyable, :long_description, :thumb_url, :full_url, :name, :on_special, :part_number, :release_date, :short_description, :withdraw_date, :occasion
has_and_belongs_to_many :categories
has_many :merch_associations
has_many :assoc_products, :through => :merch_associations
searchable do
text :aux_description, :long_description, :name, :on_special, :part_number, :short_description
text :categories do
categories.map { |category| category.name }
end
text :occasion
string :categories, :multiple => true do
categories.map {|category| category.name }
end
string :occasion
time :release_date
end
end
class MerchAssociation < ActiveRecord::Base
attr_accessible :assoc_product, :product_id
belongs_to :product
belongs_to :assoc_product, :class_name => "Product"
end
Then in rails console:
Product.find(1).assoc_products.create(Product.last, :association_type => 'up_sell')
Product Load (0.3ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 1 LIMIT 1
Product Load (0.3ms) SELECT `products`.* FROM `products` ORDER BY `products`.`id` DESC LIMIT 1
(0.0ms) BEGIN
(0.1ms) ROLLBACK
NoMethodError: undefined method `stringify_keys' for #<Product:0x007fc20d1ffd70>....
You can’t add object to relation by create,
createmethod is for creating new objects with provided attributes. Read here: undefined method `stringify_keys!' ruby on railsUse
Product.find(1).assoc_product << Product.lastinstead.What is
association_typeparam in your example? I guess you want to add related object with attributes forMerchAssocation? If so write: