我有一个模型item has_many ratings
,并且ratings belongs_to item
ratings belongs_to user
我想强制创建项目的用户也对其进行评分。其他用户可以稍后对其进行评分。item 和 user 在我的模型中没有关联。我在我的 item_spec 中执行以下操作,这在下面的no implicit conversion of Symbol into Integer
行中给了我一个错误@item = Item.new(name: "Item1",
。
class Item < ActiveRecord::Base
has_many :ratings, dependent: :destroy, inverse_of: :item
accepts_nested_attributes_for :ratings, :allow_destroy => true
validates :name , :length => { minimum: 3 }
validates :category , :length => { minimum: 3 }
validates_presence_of :ratings
end
require 'spec_helper'
describe Item do
before do
@item = Item.new(name: "Item1",
url: "www.item1.com",
full_address: "Item1Address",
city: "Item1City",
country: "Item1Country",
category: "Item1Type",
ratings_attributes: {"rating" => "3", "comment" => "Ahh Good"} )
end
也使用 FactoryGirl 我正在做这样的事情
factory :item do
before_create do |r|
r.ratings<< FactoryGirl.build(:ratings, item: r )
end
name "Item1"
url "www.Item1.com"
full_address "Item1Address"
city "Item1City"
country "Item1Country"
category "Item1Category"
end
factory :ratings do
rating 3
comment "Its not that bad"
user
end
end
这再次没有产生预期的结果。
谁能帮我解决这个问题。谢谢!