我按照文档,手动创建迁移文件和模型。我的应用中已有用户模型:
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
我在沙箱模式下运行我的控制台并执行:
group = Group.new
user = User.new
group.add user
我的控制台喷出:
NameError: uninitialized constant User::GroupMembership
发生了什么?
答案 0 :(得分:0)
已解决:类GroupMembership.rb是用驼峰式编写的,因此文件名必须是:
group_membership.rb