Rails 单一 Table 继承设计助手
Rails Single Table Inheritance Devise helpers
我用 Devise 创建了一个 User
模型并添加了一个 type
列等,因此模型 Student
和 Teacher
可以继承它。所有这一切都非常有效。我的 Teacher
模型与模型 Course
具有一对多关系,其中存储了有关教师课程的所有数据。
我的问题:Devise 助手 current_user.courses
不起作用,因为 courses
table 没有列 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
运行:
rails generate migration AddUserReferenceToCourses user:references
我认为这将创建一个迁移,将 user_id 添加到课程
您可以向课程添加迁移 table:
rails g migration add_user_id_to_courses user:references
这将在 course.rb 文件中添加 belongs_to :user
。然后转到user.rb文件并添加:
has_many :courses
此关系将允许您从课程中调用用户或从用户中调用课程。
您可以这样定义自己的助手:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_teacher, :current_student,
:teacher_logged_in?, :student_logged_in?
private
def current_teacher
@current_teacher ||= current_user if user_signed_in? and current_user.class.name == "Teacher"
end
def current_student
@current_student ||= current_user if user_signed_in? and current_user.class.name == "Student"
end
def teacher_logged_in?
@teacher_logged_in ||= user_signed_in? and current_teacher
end
def student_logged_in?
@student_logged_in ||= user_signed_in? and current_student
end
end
我没有执行过这些语法,但我过去写过类似的东西,所以如果您遇到任何语法错误,请 post 在评论中指出。
编辑:
看到您更新后的模型代码后,我认为将用户模型中的 course
关联更改为喜欢以下对您有用:
has_many :courses, :foreign_key => "teacher_id"
我用 Devise 创建了一个 User
模型并添加了一个 type
列等,因此模型 Student
和 Teacher
可以继承它。所有这一切都非常有效。我的 Teacher
模型与模型 Course
具有一对多关系,其中存储了有关教师课程的所有数据。
我的问题:Devise 助手 current_user.courses
不起作用,因为 courses
table 没有列 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
运行:
rails generate migration AddUserReferenceToCourses user:references
我认为这将创建一个迁移,将 user_id 添加到课程
您可以向课程添加迁移 table:
rails g migration add_user_id_to_courses user:references
这将在 course.rb 文件中添加 belongs_to :user
。然后转到user.rb文件并添加:
has_many :courses
此关系将允许您从课程中调用用户或从用户中调用课程。
您可以这样定义自己的助手:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_teacher, :current_student,
:teacher_logged_in?, :student_logged_in?
private
def current_teacher
@current_teacher ||= current_user if user_signed_in? and current_user.class.name == "Teacher"
end
def current_student
@current_student ||= current_user if user_signed_in? and current_user.class.name == "Student"
end
def teacher_logged_in?
@teacher_logged_in ||= user_signed_in? and current_teacher
end
def student_logged_in?
@student_logged_in ||= user_signed_in? and current_student
end
end
我没有执行过这些语法,但我过去写过类似的东西,所以如果您遇到任何语法错误,请 post 在评论中指出。
编辑:
看到您更新后的模型代码后,我认为将用户模型中的 course
关联更改为喜欢以下对您有用:
has_many :courses, :foreign_key => "teacher_id"