有人可以告诉我是否只是以错误的方式进行设置吗?
我有以下具有has_many.through关联的模型:
class Listing < ActiveRecord::Base
attr_accessible ...
has_many :listing_features
has_many :features, :through => :listing_features
validates_presence_of ...
...
end
class Feature < ActiveRecord::Base
attr_accessible ...
validates_presence_of ...
validates_uniqueness_of ...
has_many :listing_features
has_many :listings, :through => :listing_features
end
class ListingFeature < ActiveRecord::Base
attr_accessible :feature_id, :listing_id
belongs_to :feature
belongs_to :listing
end
我正在使用Rails 3.1.rc4,FactoryGirl 2.0.2,factory_girl_rails 1.1.0和rspec。这是我对:listing
工厂的基本rspec rspec完整性检查:
it "creates a valid listing from factory" do
Factory(:listing).should be_valid
end
这是Factory(:listing)
FactoryGirl.define do
factory :listing do
headline 'headline'
home_desc 'this is the home description'
association :user, :factory => :user
association :layout, :factory => :layout
association :features, :factory => :feature
end
end
在:listing_feature
和:feature
工厂也同样设置。
如果该association :features
行被注释掉,那么我所有的测试都通过了。
几时
association :features, :factory => :feature
错误消息
undefined method 'each' for #<Feature>
对我来说很有意义,因为因为listing.features
返回了一个数组。所以我改成
association :features, [:factory => :feature]
我现在得到的错误ArgumentError: Not registered: features
是这样生成工厂对象是否不明智,还是我缺少了什么?非常感谢您提供的所有信息!