Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 Rails 3与STI之间有许多连接_Ruby On Rails 3_Has Many Through_Single Table Inheritance - Fatal编程技术网

Ruby on rails 3 Rails 3与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

以下是我的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表。您可以只为用户或运动使用外键,具体取决于它们之间的关系

用户模型将需要与运动模型的指定关系,反之亦然

有关这方面的更多信息,请参见:

它试图提取运动员id而不是用户id是有道理的,因为您正在调用运动员对象

作为旁注:不需要编写
运动员.all.last
——只需编写
运动员.last

athlete = Athlete.all.last
athlete.sports