Ruby on rails 3 Rails 3与STI之间有许多连接
以下是我的STI模型:Ruby on rails 3 Rails 3与STI之间有许多连接,ruby-on-rails-3,has-many-through,single-table-inheritance,Ruby On Rails 3,Has Many Through,Single Table Inheritance,以下是我的STI模型: class User < ActiveRecord::Base end class Athlete < User has_many :sports, :through => :user_sports has_many :user_sports end class Coach < User end 生成的SQL试图使用运动员id而不是用户id。。。不太确定我做错了什么。。。任何建议都将是伟大的 我不知道你为什么有一张UserSports
class User < ActiveRecord::Base
end
class Athlete < User
has_many :sports, :through => :user_sports
has_many :user_sports
end
class Coach < User
end
生成的SQL试图使用
运动员id
而不是用户id
。。。不太确定我做错了什么。。。任何建议都将是伟大的 我不知道你为什么有一张UserSports表。您可以只为用户或运动使用外键,具体取决于它们之间的关系
用户模型将需要与运动模型的指定关系,反之亦然
有关这方面的更多信息,请参见:
它试图提取运动员id而不是用户id是有道理的,因为您正在调用运动员对象
作为旁注:不需要编写运动员.all.last
——只需编写运动员.last
athlete = Athlete.all.last
athlete.sports