首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有具有belongs_to class_name关系的此类列

没有具有belongs_to class_name关系的此类列
EN

Stack Overflow用户
提问于 2013-06-05 08:45:40
回答 2查看 540关注 0票数 1

SQLite3::SQLException: no such column: organizations.user_id: SELECT "organizations".* FROM "organizations" WHERE "organizations"."user_id" = 2

我已经这样设置了我的模型:

用户:

User has_many :organizations

组织:

代码语言:javascript
复制
attr_accessible :name, :founder, :founder_id
belongs_to :founder, :class_name => 'User'

模式:

代码语言:javascript
复制
create_table "organizations", :force => true do |t|
t.string   "name"
t.integer  "founder_id"

当我在rails-admin中编辑用户时,我收到以下消息:

代码语言:javascript
复制
`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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-05 09:38:51

您需要指定从User检索组织时要使用的列,如下所示:

代码语言:javascript
复制
class User < ActiveRecord::Base
  has_many :organizations, foreign_key: :founder_id

  #...
end
票数 2
EN

Stack Overflow用户

发布于 2013-06-05 13:32:35

组织模型属于用户,因此rails将自动使用用户的小写类名+_id(user_id)作为foreign_key。由于您的组织模型没有user_id而是founder_id,因此您需要显式地将founder_id指定为foreign_key。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16929802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档