Undefined `each 'method in factory_girl / rspec2 script

I am trying Factory message related to voting. So Post.votes will generate a voice message that is associated with it.

Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.association :votes, :factory => :vote
end

And my rspec2 is relatively simple:

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 he 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

+5
source share
2 answers
Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.association :votes, :factory => :vote
end

Same as search

some_voted_post.votes = Factory(:vote)

Basically you are trying to assign one vote as an array.

EDIT

You may have an array containing one vote, but you cannot just vote.

This is the difference between:

some_voted_post.votes = Factory(:vote)

and

some_voted_post.votes = [Factory(:vote)]

, , , .

+8

has_many, , , :

Factory.define :voted_post, :parent => :post, :class => Post do |p|
  p.votes { |vote| [vote.association(:vote)] }
end

[], ,

+4

All Articles