Я создаю пользователей и назначаю роли, используя has_many :through
ассоциацию, в соответствии с соглашением об именах. Если я ошибаюсь или какие-либо улучшения могут быть сделаны, пожалуйста, не стесняйтесь направлять меня
create_table "roles", force: :cascade do |t|
t.string "name"
t.boolean "active"
t.integer "counter"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "photo"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Я создал ассоциацию, используя следующую команду
rails g model UserRole role:references user:references
create_table "user_roles", force: :cascade do |t|
t.integer "role_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["role_id"], name: "index_user_roles_on_role_id"
t.index ["user_id"], name: "index_user_roles_on_user_id"
end
class Role < ActiveRecord::Base
has_many :user_roles
has_many :users, through: :user_roles
end
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
end
class UserRole < ApplicationRecord
belongs_to :user
belongs_to :role
end
Когда я запускаю консоль со следующим:
r1=Role.create(name:"admin",active:true)
r2=Role.create(name:"player",active:true)
u1 = User.create(first_name:"alex", roles: [r1,r2])
Я получаю следующую ошибку:
Traceback (most recent call last):
2: from (irb):3
1: from app/models/user_role.rb:1:in `<main>' NameError (uninitialized constant ApplicationRecord)
Я новичок в рельсах, пожалуйста, помогите мне с правильным руководством