我有一个模型与另一个模型has_one
关联。through
class Publisher
has_many :books
end
class Book
belongs_to :publisher
has_one :author
end
class Author
belongs_to :book
has_one :publisher, :through => :book
end
在我的 Rails 代码中,我可以author.publisher
毫无问题地调用,所以一切正常。但是在我的规范中(使用 Rspec 和 FactoryGirl,该关联似乎不起作用。这是我的 FactoryGirl 定义:
Factory.define :author do |a|
a.association :book
end
Factory.define :book do |b|
b.association :publisher
end
Factory.define :publisher
end
(省略了工厂的大部分属性)。
现在在我的规格中,我可以执行以下操作
pub = Factory(:publisher)
book = Factory(:book, :publisher => pub)
author = Factory(:author, :book => book)
author.book # => Returns book
author.book.publisher # => Returns publisher
author.publisher # => nil
那么为什么我的through
协会不起作用?