开发者

Single Table Inheritance with Join Table has_one association is returning a method_missing error

I'm having a problem getting my data model to work correctly.

I have the following models and sub models.

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

class User < ActiverRecord::Base    #Single table inheritanc开发者_C百科e
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

I have successfully created horses, owners, trainers and connections with all the type fields populated correctly. When I type Horse.all at the console it returns all horses as expected. The same is true of User.all Connections.all Trainer.all and Owner.all.

Now, however, I'm trying to do something like. Horse.trainer and Horse.owner to return the trainer and owner for the horse. When I attempt this I am getting a method_missing error. I thought that the associations I created would allow this to work. I've been banging my head on the wall now for quite a while so if anyone can offer any insight or guidance I would appreciate it.


Try doing this:

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

Do the same for Owner. That should connect the associations once and for all.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜