Rails Fixtures Don't Seem to Support Transitive Associations
Posted
by
Rick Wayne
on Stack Overflow
See other posts from Stack Overflow
or by Rick Wayne
Published on 2011-01-07T18:20:28Z
Indexed on
2011/01/07
21:53 UTC
Read the original article
Hit count: 589
So I've got 3 models in my app, connected by HABTM relations:
class Member < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :software_instances
end
class SoftwareInstance < ActiveRecord::Base
end
And I have the appropriate join tables, and use foxy fixtures to load them:
-- members.yml --
rick:
name: Rick
groups: cals
-- groups.yml --
cals:
name: CALS
software_instances: delphi
-- software_instances.yml --
delphi:
name: No, this is not a joke, someone in our group is still maintaining Delphi apps...
And I do a rake db:fixtures:load, and examine the tables. Yep, the join tables groups_members and groups_software_instances have appropriate IDs in, big numbers generated by hashing the fixture names. Yay!
And if I run script/console, I can look at rick's groups and see that cals is in there, or cals's software_instances and delphi shows up. (Likewise delphi knows that it's in the group cals.) Yay!
But if I look at rick.groups[0].software_instances...nada. []. And, oddly enough, rick.groups[0].id is something like 30 or 10, not 398398439.
I've tried swapping around where the association is defined in the fixtures, e.g.
-- members.yml --
rick
name: rick
-- groups.yml --
cals
name: CALS
members: rick
No error, but same result.
(Later: Confirmed, same behavior on MySQL. In fact I get the same thing if I take the foxification out and use ERB to directly create the join tables, so:
-- license_groups_software_instances --
cals_delphi: group_id: <%= Fixtures.identify 'cals' %> software_instance_id: <%= Fixtures.identify 'delphi' $>
Before I chuck it all, reject foxy fixtures and all who sail in her and just hand-code IDs...anybody got a suggestion as to why this is happening? I'm currently working in Rails 2.3.2 with sqlite3, this occurs on both OS X and Ubuntu (and I think with MySQL too).
TIA, my compadres.
© Stack Overflow or respective owner