2013-06-05 34 views
0

SQLite3::SQLException: no such column: organizations.user_id: SELECT "organizations".* FROM "organizations" WHERE "organizations"."user_id" = 2沒有這樣的列有belongs_to的CLASS_NAME關係

我已經安裝我的模型是這樣的:

用戶:

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" 

當我去編輯在軌管理員用戶,我得到這個消息:

`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

回答

1

你需要指定從用戶獲取組織的時候,像這樣使用的柱:

class User < ActiveRecord::Base 
    has_many :organizations, foreign_key: :founder_id 

    #... 
end 
0

組織模式是屬於用戶,所以Rails會自動使用用戶的小寫類名+ _id(user_id)作爲foreign_key。 由於您的組織模型不具有user_id而只有founder_id,因此您需要明確指定founder_id作爲foreign_key。

相關問題