开发者

Rails Fixtures Don't Seem to Support Transitive Associations

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开发者_如何学Python 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' $>

(Later still: GAAA, it's even worse than THAT! If I construct a query against the database, everything hooks up, but if I run script/console or a unit test, the second hop of the association chain is broken, as described above. So maybe it has nothing whatever to do with fixtures.)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜