Children with the same Parents

33 views
Skip to first unread message

Jan Jezek

unread,
Jan 17, 2014, 8:29:45 AM1/17/14
to fabrica...@googlegroups.com
Hi there, 

How can I make a bunch of "childrens" (in my case Votings) with the same Parent (in my case User, Answer, Question, Survey)?

I wanna provide a "special set of Votings" for my tests.

Fabricator :voting do

end

Fabricator :full_voting, from: :voting do
  participant { Fabricate :participant_basic }
  survey
  question
  answer
  department { Fabricate :department }
  user
  answer1 1
  answer2 0
  answer3 -1
end

This is my model:

class Voting < ActiveRecord::Base

  attr_accessible :survey_id,
                  :department_id,
                  :question_id,
                  :answer_id,
                  :answer1,
                  :answer2,
                  :answer3

  belongs_to :participant

  belongs_to :user
  belongs_to :department
  belongs_to :industry
  belongs_to :survey
  belongs_to :question
  belongs_to :answer
  #has_many :opinions
  #has_many :answers, through: :opinions

  validates_presence_of :participant, :user, :survey, :question, :answer1, :answer2, :answer3
end

I know I can do something like:

Fabrcator :answer do
  voting(count: 100) {Fabricate :voting)
end

But this does not work because, Voting needs also a User, Question and so on and they are always newly generated with each call. 

Please help me out of that maze.

many thanks in advance

Paul Elliott

unread,
Jan 17, 2014, 9:21:16 AM1/17/14
to fabrica...@googlegroups.com
If I understand your question correctly, you need the user for this voting to be the same as the user for the survey, question, and answer. To do that, you can structure your fabricator like this.

```
Fabricator :full_voting, from: :voting do
  participant(fabricator: :participant_basic)
  user
  survey { |attrs| Fabricate(:survey, user: attrs[:user]) }
  question { |attrs| Fabricate(:question, user: attrs[:user]) }
  answer { |attrs| Fabricate(:answer, user: attrs[:user]) }
  department
  answer1 1
  answer2 0
  answer3 -1
end
```


-- Paul


--
You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages