简体   繁体   中英

Groupify NameError: uninitialized constant User::GroupMembership

I followed the documentation, manually creating the migration file and the models. I already had a User model in my app:

user.rb:

class User < ActiveRecord::Base
    authenticates_with_sorcery!

    validates :name, presence: true
    validates :password, length: { minimum: 4 }
    validates :password, confirmation: true
    validates :password_confirmation, presence: true

    validates :email, uniqueness: true

    has_many :testimonials
    has_many :materials

    groupify :group_member
    groupify :named_group_member
end

class Assignment < ActiveRecord::Base
  groupify :group_member
end

group.rb:

class Group < ActiveRecord::Base  
  groupify :group, members: [:users, :assignments], default_members: :users
end

GroupMembership.rb:

class GroupMembership < ActiveRecord::Base  
  groupify :group_membership
end

schema.rb:

ActiveRecord::Schema.define(version: 20150301133633) do

  create_table "group_memberships", force: :cascade do |t|
    t.string  "member_type"
    t.integer "member_id"
    t.integer "group_id"
    t.string  "group_name"
    t.string  "membership_type"
  end

  add_index "group_memberships", ["group_id"], name: "index_group_memberships_on_group_id"
  add_index "group_memberships", ["group_name"], name: "index_group_memberships_on_group_name"
  add_index "group_memberships", ["member_id", "member_type"], name: "index_group_memberships_on_member_id_and_member_type"

  create_table "groups", force: :cascade do |t|
    t.string "type"
  end

  create_table "materials", force: :cascade do |t|
    t.string   "level"
    t.text     "description"
    t.string   "link"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.string   "title"
    t.integer  "user_id"
  end

  add_index "materials", ["user_id"], name: "index_materials_on_user_id"

  create_table "testimonials", force: :cascade do |t|
    t.text     "content"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "testimonials", ["user_id", "created_at"], name: "index_testimonials_on_user_id_and_created_at"
  add_index "testimonials", ["user_id"], name: "index_testimonials_on_user_id"

  create_table "tests", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",            null: false
    t.string   "crypted_password"
    t.string   "salt"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true

end

I run my console in sandbox mode and do:

group = Group.new
user = User.new

group.add user

And my console spews out:

NameError: uninitialized constant User::GroupMembership

What's going on?

SOLVED: The class GroupMembership.rb is written in camel case, so the filename needs to be:

group_membership.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM