SQLite3::SQLException: no such column: organizations.user_id: SELECT "organizations".* FROM "organizations" WHERE "organizations"."user_id" = 2
我已经这样设置了我的模型:
用户:
User has_many :organizations
组织:
attr_accessible :name, :founder, :founder_id
belongs_to :founder, :class_name => 'User'模式:
create_table "organizations", :force => true do |t|
t.string "name"
t.integer "founder_id"当我在rails-admin中编辑用户时,我收到以下消息:
`SQLite3::SQLException: no such column: organizations.user_id: SELECT "organizations".* FROM "organizations" WHERE "organizations"."user_id" = 2`我想访问组织上的方正,其中方正是用户。看起来,rails_admin在寻找创始人的时候,却在寻找user_id。
上一个问题:Can access _id of a references object but not the object directly
发布于 2013-06-05 09:38:51
您需要指定从User检索组织时要使用的列,如下所示:
class User < ActiveRecord::Base
has_many :organizations, foreign_key: :founder_id
#...
end发布于 2013-06-05 13:32:35
组织模型属于用户,因此rails将自动使用用户的小写类名+_id(user_id)作为foreign_key。由于您的组织模型没有user_id而是founder_id,因此您需要显式地将founder_id指定为foreign_key。
https://stackoverflow.com/questions/16929802
复制相似问题