小编adi*_*tri的帖子

与同一模型的多个关联

我有两个类,我想指定如下:

class Club < ActiveRecord::Base
  belongs_to :president, :class_name => "Person", :foreign_key => "president_id"
  belongs_to :vice_president, 
             :class_name => "Person",
             :foreign_key => "vice_president_id"
end

class Person < ActiveRecord::Base
  has_one :club, :conditions => 
  ['president_id = ? OR vice_president_id = ?', '#{self.id}', '#{self.id}']
end
Run Code Online (Sandbox Code Playgroud)

这不起作用,并在尝试从人物对象获得俱乐部关联时给出错误.错误是因为我在查看SQL时在俱乐部表中查找person_id.我可以通过声明多个has_one关联来解决它,但感觉这是不正确的做法.

一个人只能是一个俱乐部的总裁或副总裁.

任何能够就此问题提供一些建议的人,我都会非常感激.

activerecord ruby-on-rails

6
推荐指数
1
解决办法
3696
查看次数

标签 统计

activerecord ×1

ruby-on-rails ×1