【问题标题】:Rails single through table for multiple models用于多种型号的导轨单通表
【发布时间】:2018-04-10 11:51:14
【问题描述】:

每当我尝试在 user_grade 表中添加任何用户的角色(比如管理员)和等级时,它都会给出错误,其他角色必须存在(老师、学生、监护人)。我不知道如何解决这个问题,因为这是有点复杂的关系结构。 或者任何人都可以提出更好的方法吗?

class Admin < User

  # can post many posts
  has_many :posts, dependent: :destroy , foreign_key: 'user_id'

  # admin post can have many tags
  has_many :post_tags, dependent: :destroy, foreign_key: 'user_id'
  has_many :tags , through: :post_tags

  # can have many grades to see other grades posts
  has_many :user_grades, foreign_key: 'user_id'
  has_many :grades, through: :user_grades

end

class Teacher < User

  # can post many posts
  has_many :posts, dependent: :destroy , foreign_key: 'user_id'

  # teacher post can have many tags
  has_many :post_tags, dependent: :destroy, foreign_key: 'user_id'
  has_many :tags , through: :post_tags

  # can teach many grades
  has_many :user_grades, dependent: :destroy, foreign_key: 'user_id'
  has_many :grades , through: :user_grades


end

class Student < User


  # a student can only in particular grade
  has_one :user_grade , dependent: :destroy, foreign_key: 'user_id'
  has_one :grade , through: :user_grade

end

class Guardian < User

  # parents can have many children in different classes
  has_many :user_grades, dependent: :destroy, foreign_key: 'user_id'
  has_many :grades, through: :user_grades


end

class Grade < ApplicationRecord

  has_many :user_grades, dependent: :destroy
  has_many :admins, through: :user_grades

  has_many :teachers, through: :user_grades

  has_many :students , through: :user_grades

  has_one :guardian, through: :user_grade

end


class UserGrade < ApplicationRecord

  belongs_to :grade
  belongs_to :admin
  belongs_to :teacher
  belongs_to :student
  belongs_to :guardian


end

数据库:

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

  create_table "grades", force: :cascade do |t|
    t.integer "cls"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "post_tags", force: :cascade do |t|
    t.integer "user_id"
    t.integer "post_id"
    t.integer "tag_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

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

  create_table "roles", force: :cascade do |t|
    t.string "name"
    t.string "resource_type"
    t.integer "resource_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id"
    t.index ["name"], name: "index_roles_on_name"
    t.index ["resource_type", "resource_id"], name: "index_roles_on_resource_type_and_resource_id"
  end

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

  create_table "user_grades", force: :cascade do |t|
    t.integer "user_id"
    t.integer "grade_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  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.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "unconfirmed_email"
    t.integer "failed_attempts", default: 0, null: false
    t.string "unlock_token"
    t.datetime "locked_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "type"
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
  end

  create_table "users_roles", id: false, force: :cascade do |t|
    t.integer "user_id"
    t.integer "role_id"
    t.index ["role_id"], name: "index_users_roles_on_role_id"
    t.index ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id"
    t.index ["user_id"], name: "index_users_roles_on_user_id"
  end

end

我在这里使用 rolify 和 cancancan gem 作为用户角色。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 relationships jointable single-table-inheritance


    【解决方案1】:

    尝试将optional 参数添加到belongs_to 关系

    class UserGrade < ApplicationRecord
       belongs_to :grade
       belongs_to :admin, optional: true
       belongs_to :teacher, optional: true
       belongs_to :student, optional: true
       belongs_to :guardian, optional: true
    end
    

    Rails 5 默认需要 belongs_to 关联。我们可以通过添加optional: true 来避免对belongs_to 关系的验证。

    您可以通过将 belongs_to_required_by_default 的值保持为 false 来关闭所有模型的此行为。

    # file => config/application.rb
    config.active_record.belongs_to_required_by_default = false
    

    【讨论】:

    • 我应该在哪里添加这一行? Rails.application.config.active_record.belongs_to_required_by_default = false
    • 如果添加此配置,它将全局应用于所有模型。如果您只需要少数型号,则可以继续使用第一个选项。
    猜你喜欢
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多