我正在研究Ruby on rails 2.3.8,并且我想构建一个功能来跟踪用户。关注用户逻辑
为此,我创建了2个表:用户和以下,用他们各自的模型:
class User < ActiveRecord::Base
has_and_belongs_to_many :followings, :foreign_key => "follower_id"
end
class Following < ActiveRecord::Base
has_and_belongs_to_many :users, :foreign_key => "follower_id", :class_name => "User"
end
现在,当我尝试执行current_user.followings.all
(凭有效CURRENT_USER的当然),它会抛出以下例外:
'followings_users' doesn't exist: SELECT 'followings'.* FROM `followings` INNER JOIN 'followings_users' ON 'followings'.id = 'followings_users'.following_id WHERE ('followings_users'.follower_id = 1)
我无法完成此项工作。我不知道为什么它要求我提供“followings_users”表。如果我想称之为“跟随”,该怎么办?
我在这里做错了什么?
因此,我最好的选择是分别更改following_user和following_users的模型和表名称? – 2010-07-29 17:53:38
您只需添加':join_table'选项即可。当我们不使用传统命名时,我们可以使用这个选项来指定连接表名。 – 2010-07-29 17:57:28
我应该在哪个模型中添加该选项?它应该是:join_table =>'followings'?因为我已经在User和Following模型上试过了,它向我投掷了以下内容,因为它试图“自己加入”:不是唯一的表/别名:'followings':SELECT'followings'。* FROM'followings' INNER JOIN 'followings' ON'followings'.id ='followings'.following_id WHERE('followings'.follower_id = 1) – 2010-07-29 18:01:45