NoMethodError:“User”的未定义方法`arel_table':String

时间:2015-04-11 12:08:10

标签: ruby-on-rails ruby associations

Rails 4.2

g.user_curator

NoMethodError: undefined method `arel_table' for "User":String

我的表格:

create_table "groups", force: :cascade do |t|
    t.string   "name",            limit: 255, default: "", null: false
    t.integer  "user_curator_id", limit: 4
    t.integer  "user_leader_id",  limit: 4
    t.datetime "created_at",                               null: false
    t.datetime "updated_at",                               null: false
 end

create_table "users", force: :cascade do |t|
    t.string   "email",                  limit: 255, default: "", null: false
    t.string   "encrypted_password",     limit: 255, default: "", null: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "access_level",           limit: 4,   default: 0
    t.string   "first_name",             limit: 255
    t.string   "last_name",              limit: 255
    t.string   "middle_name",            limit: 255
  end

我的模特:

class User < ActiveRecord::Base
  has_one :user_curator, :class_name => 'Group'
end

class Group < ActiveRecord::Base
  belongs_to :user_curator, :class => 'User', foreign_key: "user_curator_id"
end

帮助正确设置表的关联。在该组成为策展人和领导者(表用户)

1 个答案:

答案 0 :(得分:2)

class Group < ActiveRecord::Base
  belongs_to :curator, class_name: 'User', foreign_key: 'user_curator_id'
end

class User < ActiveRecord::Base
  has_one :group, foreign_key: 'user_curator_id'
end

这将工作