Nightwolf
Nightwolf

Reputation: 4697

Factory Girl - overriding attributes of belongs_to object

I'm trying to create a Meeting object, and while doing so override the attribute of the Course object which the Meeting object belongs_to:

Here are my models:

class Course < ActiveRecord::Base
  attr_accessible :name, :description
end

class Meeting < ActiveRecord::Base
  attr_accessible :name
  belongs_to :course
end

Here are my definitions:

factory :course, class: Course do
    name      "Generic Course Name"
    description: "Very cheap"
end

factory :meeting, class: Meeting do
  name "Meeting name"
  course
end

Here's what I'm trying to do:

FactoryGirl.create(:meeting) do |meeting| 
    meeting.course.name = "other name"
end

However, I can't seem to override the course name from its default. I can override the meeting name just fine by simply replacing meeting.course.name = "other name" with meeting.name = "other name", but accessing the belongs_to object doesn't have the desired effect.

Any help would be appreciated.

Thank you.

Upvotes: 1

Views: 1828

Answers (1)

DVG
DVG

Reputation: 17480

What about?

course = create(:course, name: "My Course Name")
create(:meeting, course: course)

Upvotes: 3

Related Questions