具有连接表的单表继承has_one关联返回method_missing错误

时间:2011-03-13 22:20:52

标签: ruby-on-rails join associations single-table-inheritance

我在使数据模型正常工作时遇到问题。

我有以下模型和子模型。

class Horse < ActiveRecord::Base
has_one :connection
has_one :trainer, :through => :connection
has_one :owner, :through => :connection
end

class User < ActiverRecord::Base    #Single table inheritance
has_many :connections
has_many :horses, :through => :connections
end

class Owner < User
#owner specific code
end

class Trainer < User
#trainer specific code
end

class Connection < ActiveRecord::Base #join table
belongs_to :horse
belongs_to :owner
belongs_to :trainer
end

我已经成功创建了匹配正确填充的所有类型字段的马匹,所有者,培训师和连接。当我在控制台键入Horse.all时,它会按预期返回所有匹配的马。 User.all Connections.all Trainer.all和Owner.all也是如此。

然而,现在,我正在尝试做类似的事情。 Horse.trainer和Horse.owner返回训练师和马的主人。当我尝试这个时,我得到一个method_missing错误。我认为我创建的关联将允许这个工作。我现在已经在墙上撞了很长一段时间,所以如果有人能提供任何见解或指导,我会很感激。

1 个答案:

答案 0 :(得分:2)

尝试这样做:

class Trainer < User
  has_many :connections
  has_many :horses, :through => :connections
end

为所有者做同样的事情。这应该一劳永逸地联系起来。

相关问题