Creating fixtures for has_many :through

I’m stymied at how to create a fixture that establishes a has_many
:through relationship.

I’ve watched the railscast at:
http://media.railscasts.com/videos/081_fixtures_in_rails_2.mov
… but that’s for HABTM relationships. I’ve read:
http://www.ruby-forum.com/topic/145676
but that ultimately doesn’t answer any question. So with no further
ado:

==== The models:
class User < ActiveRecord::Base
has_many :user_roles, :dependent => :destroy
has_many :roles, :through => :user_roles
end
class Role < ActiveRecord::Base
has_many :user_roles, :dependent => :destroy
has_many :users, :through => :user_roles
end
class UserRole < ActiveRecord::Base
belongs_to :role
belongs_to :user
end
==== The schema:
create_table “users”, :force => true do |t|
t.string “name”, :default => “”, :null => false
t.string “email”, :default => “”, :null => false
end
create_table “roles”, :force => true do |t|
t.string “name”
end
create_table “user_roles”, :force => true do |t|
t.integer “role_id”
t.integer “user_id”
end

I understand that you cannot directly add roles in a user fixture, i.e.
the following does NOT work:

==== file: fixtures/roles.yml
administrator:
name: administrator
user:
name: user
guest:
name: guest
==== file: fixtures/users.yml
superuser:
name: “Superuser”
email: [email protected]
roles: administrator, user

… since YAML will try to directly write to the nonexistent user.roles
column. But I WOULD expect that you could write a user_roles.yaml file
to make explicit associations, along the lines of:

==== file: fixtures/roles.yml
administrator:
name: administrator
user:
name: user
guest:
name: guest
==== file: fixtures/users.yml
superuser:
name: “Superuser”
email: [email protected]
==== file: fixtures/user_roles.yml
one:
user: users(:superuser)
role: roles(:administrator)
two:
user: users(:superuser)
role: roles(:user)

… but no combination of “roles(:user)”, “roles(:user).find”,
“roles(:user).find.id” etc appears to create the correct associations.

Before Marnen tells me to put aside my Luddite tendencies and that I
should learn factory_girl or Machinist or the next testing framework du
jour, is there any sensible way to do this using fixtures?

  • ff

Fearless F. wrote:

UPDATE!

In a fit of gentle snarkiness, Fearless F. wrote:

Before Marnen tells me to put aside my Luddite tendencies and that I
should learn factory_girl or Machinist or the next testing framework du
jour, is there any sensible way to do this using fixtures?

The more I’ve looked into it, the more I think Marnen would be justified
in telling me to put aside my Luddite tendencies: the fixtures framework
creates an arbitrary (and brittle) separation between test data and test
functions.

Exactly! That’s a big reason that fixtures suck. It’s much nicer to
use factories to create test data where you need it.

I’m currently grabbing the gems for RSpec and factory_girl.

You may find Machinist easier to work with than FG. Also check out
Faker, which is very useful with any fixture gem.

So there!

  • ff (channeling for Marnen :slight_smile:

LOL! Well played. This is apparently my week to be snarked on the
list. :stuck_out_tongue:

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

UPDATE!

In a fit of gentle snarkiness, Fearless F. wrote:

Before Marnen tells me to put aside my Luddite tendencies and that I
should learn factory_girl or Machinist or the next testing framework du
jour, is there any sensible way to do this using fixtures?

The more I’ve looked into it, the more I think Marnen would be justified
in telling me to put aside my Luddite tendencies: the fixtures framework
creates an arbitrary (and brittle) separation between test data and test
functions.

I’m currently grabbing the gems for RSpec and factory_girl.

So there!

  • ff (channeling for Marnen :slight_smile: