我有一個屬於關係的模型。Ruby On Rails Model沒有方法
class Product < ActiveRecord::Base
attr_accessible :name, :price, :request_id, :url
# Relationships
belongs_to :request
end
class Request < ActiveRecord::Base
attr_accessible :category, :keyword
# Relationships
has_many :products
end
這是在我的控制器功能的代碼 產物= Product.where({:ASIN => ASIN})第一
# See if the product exists
begin
#This throws a method not found error for where
product = Product.where({ :name => name }).first
rescue
Product.new
# This throws a method not found error for request_id
product.request_id = request.id
product.save
end
我試圖創建一個新的產品對象等。所以 產品= Product.first(:條件=> {:名稱=>名稱})
當我打電話,我得到一個錯誤說undefined method 'first' for Product:Class
我試圖這樣做Product.new,我不能訪問任何屬性。我得到這個每個人undefined method 'request_id=' for #<Product:0x007ffce89aa7f8>
我已經能夠保存請求對象。我在做什麼產品有問題?
編輯:
所以事實證明有正被進口,這不是一個ActiveRecord類的舊產品的數據類型。它正在使用,而不是我的Product :: ActiveRecord。我刪除了這個導入,這很好。抱歉浪費了大家的時間。
不知道這個問題該怎麼處理。
'product = Product.where(:conditions => {:name => name})first' – 2013-02-26 01:50:41
你說你試圖「創建一個新產品」,但你打電話給'.first',它是用於選擇「產品」。如果你想用一個給定的名字來初始化一個新的Product,你應該使用下面的代碼:'product = Product.new(:name => name)' – 2013-02-26 01:52:00
我試圖創建一個新產品(空的)與Product.new。這不起作用。我嘗試了Product.where,並得到了這個未定義的方法'Where'for Product:Class – smokingoyster 2013-02-26 01:57:00