1

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 を探すように見えます。

Previous q: 参照オブジェクトの _id にアクセスできますが、オブジェクトに直接アクセスすることはできません

4

2 に答える 2

2

次のように、ユーザーから組織を取得するときに使用する列を指定する必要があります。

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

  #...
end
于 2013-06-05T01:38:51.100 に答える
0

組織モデルは User に属しているため、Rails は自動的に User の小文字のクラス名 +_id(user_id) をforeign_key として使用します。組織モデルには user_id ではなくfounder_id があるため、founder_id をforeign_key として明示的に指定する必要があります。

于 2013-06-05T05:32:35.147 に答える