未定义的方法“each”在factory_girl / rspec2场景中
我正在尝试创建与投票相关的帖子。这样 Post.votes 将生成与其关联的投票。
Factory.define :voted_post, :parent => :post, :class => Post do |p|
p.association :votes, :factory => :vote
end
我的 rspec2 相对简单:
describe "vote scores" do
it "should show me the total vote score" do
@post = Factory(:voted_post)
@post.vote_score.should == 1
end
end
那么为什么它会返回此错误:
Failures:
1) Post vote scores should show me the total vote score
Failure/Error: @post = Factory(:voted_post)
undefined method `each' for #<Vote:0x105819948>
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Rails 3.0.0
I'm trying to Factory a Post associated with a Vote. So that Post.votes would generate the Vote's that are associated with it.
Factory.define :voted_post, :parent => :post, :class => Post do |p|
p.association :votes, :factory => :vote
end
And my rspec2 is relatively straightforward :
describe "vote scores" do
it "should show me the total vote score" do
@post = Factory(:voted_post)
@post.vote_score.should == 1
end
end
So why would it return this error :
Failures:
1) Post vote scores should show me the total vote score
Failure/Error: @post = Factory(:voted_post)
undefined method `each' for #<Vote:0x105819948>
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Rails 3.0.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与尝试去相同
基本上您正在尝试将单个投票分配为数组。
编辑
您可以拥有一个包含单票的数组,但不能只拥有单票。
之间的区别是:
和
前者不是数组,因此不起作用,后者是数组。
Is the same as trying to go
Basically you're attempting to assign a single vote as an array.
EDIT
You can have an array containing a single vote, but you can't just have a single vote.
It's the difference between:
and
The former is not an array, and therefore does not work, the latter is an array.
如果你想分配 has_many 关联,它需要数组而不是单个值,你应该使用长格式:
并用 [] 封装关联的创建以确保返回数组
If you want to assign has_many association which expects array and not a single value, you should use the long form:
And encapsulate the creation of the association with [] to ensure that array would be returned