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

时间:2013-04-21 13:18:23

标签: 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

在某些方面,我意识到user不是一个好名字,所以我已将其更改为author

class Forum::Thread < ActiveRecord::Base
  belongs_to :forum
  belongs_to :author, class_name: 'User'
  attr_accessible :body, :title

  has_many :comments, dependent: :destroy
end

另外,在user我有以下内容:

has_many :forum_threads, class_name: 'Forum::Thread', inverse_of: :author

但是当我尝试获取线程列表(例如@user.forum_threads)时,看起来rails仍构建一个引用旧user_id而不是新author_id的查询}。

 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>

我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:1)

也许您应该使用外键和外键我的意思是“user_id”

答案 1 :(得分:0)