【发布时间】:2010-01-24 00:57:45
【问题描述】:
我有一个模型Couple,它有两列first_person_id 和second_person_id,还有一个模型Person,它的主键是person_id,列有name
这是我想要的用法:
#including 'Person' model for eager loading, this is crucial for me
c = Couple.find(:all, :include => :persons)[0]
puts "#{c.first_person.name} and #{c.second_person.name}"
那么我该怎么做呢?
【问题讨论】:
-
如果你使用 Rails,为什么 Person 模型的主键是“person_id”而不仅仅是“id”?
-
我为这篇文章更改了模型的名称,因为我想让这些名称远离互联网。它应该是
id,但这不是一个困难的修复。只需使用set_primary_key。
标签: ruby-on-rails ruby activerecord has-many