Oct 21, 2009
Defining fixtures for table without model in rails
Few days back, I need to write rspec tests for one of my ongoing projects. I had a self referential association like this:
class User
has_and_belongs_to_many :friends,
:class_name => "User",
:join_table => "friends_users",
:association_foreign_key => "friend_id",
:foreign_key => "user_id"
end
I had friends_users.yml file in my fixtures. Since I did not have any intermediate model, I could not use fixtures defined in users.yml in friends_users.yml. I was able to find a way out within Fixtures class itself. Here is the snippet from friends_users.yml
jack_and_jill:
user_id: <%= Fixtures.identify('Jack') %>
friend_id: <%= Fixtures.identify('Jill') %>
Here is snippet from users.yml:
Jack: name: Jack Brown Jill: name: Jill Green
For further details, refer to rails api documentation here
No Comments, Comment or Ping
Reply to “Defining fixtures for table without model in rails”