0

2つの商品の間に簡単な商品の関連付けを作成しようとしています。私はいくつかのチュートリアルに従い、それは正しいと思いますが、関連するアイテムを直接追加しようとすると、エラーがスローされ続けます。私はRORを初めて使用しますが、誰かが私がここで間違ったことを理解するのを手伝ってくれるでしょうか?

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

次に、Railsコンソールで:

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>....
4

1 に答える 1

0

createでリレーションにオブジェクトを追加することはできません。createメソッドは、提供された属性を持つ新しいオブジェクトを作成するためのものです。ここを読んでください:未定義のメソッド `stringify_keys! ' レール上のルビー

Product.find(1).assoc_product << Product.last代わりに使用してください。

association_typeあなたの例のparamは何ですか?MerchAssocation?の属性を持つ関連オブジェクトを追加したいと思います。もしそうなら、書いてください:

Product.find(1).merch_associations.create(:assoc_product=>Product.last,:association_type => 'up_sell')
于 2012-07-20T22:30:41.567 に答える