Ruby on rails Rails-模型关联生成无效的SQL查询

Ruby on rails Rails-模型关联生成无效的SQL查询,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个rails应用程序,其中一个模型就是这样构建的 class Forum::Thread < ActiveRecord::Base belongs_to :forum belongs_to :user attr_accessible :body, :title has_many :comments, dependent: :destroy end 此外,在用户界面中,我有以下内容: has_many :forum_threads, class_name: 'For

我有一个rails应用程序,其中一个模型就是这样构建的

class Forum::Thread < ActiveRecord::Base
  belongs_to :forum
  belongs_to :user
  attr_accessible :body, :title

  has_many :comments, dependent: :destroy
end
此外,在用户界面中,我有以下内容:

has_many :forum_threads, class_name: 'Forum::Thread', inverse_of: :author
但是,当我尝试获取threads@user.forum\u线程列表时,例如,rails似乎仍然构建了一个查询,该查询引用了旧的用户id,而不是新的作者id


我能做些什么来解决这个问题?

也许可以尝试设置外键

也许你应该使用外键,外键指的是用户id

我已经尝试在我的线程模型中包含外键:author\u id,并重新启动了rails服务器,但错误保持不变。你在has\u many关联上也这样做了吗?但是外键已经是“user_id”,这就是问题所在。我想你误解了什么。默认情况下,该方法使用您指定的参数查找外键,在您的示例中为:author查找author\u id,然后指定class\u name User,但不指定pass foreign key,您应该是User\u id。我建议阅读rails Guide 4.1.2.6,那么您的建议是什么?将表列重命名回user\u id?您是否已将author\u id列添加到user表中?哦,我明白您的意思。你说得对。有很多:论坛线程,类名:“论坛::线程”,外键:“作者id”适合我。
has_many :forum_threads, class_name: 'Forum::Thread', inverse_of: :author
 Showing /home/giladnaaman/Programming/Eclipse/Hephaestus/app/views/users/show.html.erb where line #40 raised:

SQLite3::SQLException: no such column: forum_threads.user_id: SELECT COUNT(*) FROM "forum_threads"  WHERE "forum_threads"."user_id" = 1

Extracted source (around line #40):

37:                         # of Threads
38:                     </dt>
39:                     <dd>
40:                         <%= @user.forum_threads.count%> <%= link_to 'watch', '#', class: 'btn btn-mini pull-right'%>
41:                     </dd>
42:                     <br />
43:                     <dt>