Я создал модель User
с помощью Devise и добавил столбец type
и т. д., чтобы модели Student
и Teacher
могли наследовать от него. Все это прекрасно работало. Моя модель Teacher
имеет отношение один ко многим к модели Course
, где хранятся все данные о курсах учителей.
Моя проблема: помощник Devise current_user.courses
не работает, потому что в таблице courses
нет столбца user_id
. Как сделать так, чтобы current_user
мог разрешать .courses
, даже если атрибут в курсах называется teacher_id
?
Я новичок в Rails, поэтому буду признателен за любую помощь! :)
РЕДАКТИРОВАТЬ: уточненный вопрос и добавленная схема и модели.
# schema.rb:
create_table "courses", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "teacher_id"
t.index ["teacher_id"], name: "index_courses_on_teacher_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "type"
t.integer "quiz_session_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["quiz_session_id"], name: "index_users_on_quiz_session_id"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
# /app/models/course.rb:
class Course < ApplicationRecord
belongs_to :teacher
has_many :students
delegate :teachers, :students, to: :users
end
# /app/models/user.rb:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :courses
# Which users subclass the User model
def self.types
%w(Teacher Student)
end
# Add scopes to the parent models for each child model
scope :teachers, -> { where(type: 'Teacher') }
scope :students, -> { where(type: 'Student') }
end
# /app/models/teacher.rb:
class Teacher < User
end
schema.rb
и/или модели? - person Sebastian Palma   schedule 13.05.2017