[英]Ruby on Rails: Problem with Single Table Inheritance and has_and_belongs_to_many Relationship
我正在設計我的系統架構。 為此,我有不同角色的用戶。 鑒於我想要 Devise 給出的常見行為,我決定賭單桌 Inheritance。
所以,除了普通用戶(普通客戶)之外,我還有編輯。
網絡:假設它就像一個顯示內容的固定結構。 您可以想象這些具有旋轉內容的屏幕網絡。
編輯:負責發布內容的用戶子集。 編輯需要(由管理員)預先授權才能在特定網絡中發布內容。 可能很多。 但這並不意味着每次編輯都會發帖,都會發給他授權的所有網絡。 最后,他可以選擇哪些網絡將顯示特定事件。
事件:為編輯器編寫的一段內容,可能會顯示在編寫它的編輯器有權訪問的所有網絡中。
最后,用控制台測試,我得到這個錯誤:
irb(main):001:0> E = Editor.find_by_email("info@adeter.org")
Editor Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."type" = $1 AND "users"."email" = $2 LIMIT $3 [["type", "Editor"], ["email", "info@adeter.org"], ["LIMIT", 1]]
=> #<Editor id: 3, email: "info@adeter.org", role: "editor", first_name: ni...
irb(main):002:0> E.networks
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column networks_users.editor_id does not exist)
LINE 1: ...works"."id" = "networks_users"."network_id" WHERE "networks_...
^
irb(main):003:0>
我在這里超出了我的舒適區,我真的不知道我在做什么。 最初的計划是錯誤的還是我做錯了什么?
為什么我無法訪問關系?
編輯器.rb
class Editor < User
has_and_belongs_to_many :networks
end
事件.rb
class Event < ApplicationRecord
belongs_to :editor
has_and_belongs_to_many :network
has_one_attached :image, dependent: :destroy
end
網絡.rb
class Network < ApplicationRecord
belongs_to :user
has_many :prospects, dependent: :destroy
has_many :nodes, dependent: :destroy
has_many :highlights, dependent: :destroy
has_many :companies, dependent: :destroy
has_many :promos, dependent: :destroy
has_and_belongs_to_many :editors
has_and_belongs_to_many :events
has_one_attached :header, dependent: :destroy
validates :name, presence: true, length:{ minimum: 3 }, uniqueness: true
validates :user_id, :presence => true, :if => Proc.new { user.superadmin? }
end
從 schema.rb 中提取
create_table "events", force: :cascade do |t|
t.string "title"
t.string "info"
t.datetime "date_from"
t.datetime "date_till"
t.integer "version", default: 0
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_events_on_user_id"
end
create_table "events_networks", id: false, force: :cascade do |t|
t.bigint "network_id", null: false
t.bigint "event_id", null: false
t.index ["event_id"], name: "index_events_networks_on_event_id"
t.index ["network_id"], name: "index_events_networks_on_network_id"
end
create_table "networks", force: :cascade do |t|
t.string "name"
t.integer "number_of_highlights"
t.integer "number_of_promos"
t.boolean "open_to_request", default: false
t.boolean "public", default: false
t.integer "number_of_events"
t.integer "time_between_slides"
t.integer "time_delay_with_header"
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_networks_on_user_id"
end
create_table "networks_users", id: false, force: :cascade do |t|
t.bigint "network_id", null: false
t.bigint "user_id", null: false
t.index ["network_id"], name: "index_networks_users_on_network_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.integer "role", default: 0
t.string "first_name"
t.string "last_name"
t.date "birthdate"
t.string "id_number"
t.string "vat_number"
t.string "vat_company_name"
t.string "phone"
t.string "address"
t.integer "current_company_id"
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 "type"
t.integer "failed_attempts", default: 0, null: false
t.datetime "locked_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
問題是,當使用繼承的 model 時,rails 不知道從哪里來。 解決方案是指定所有內容:外鍵和連接表。
class Editor < User
has_and_belongs_to_many :networks, join_table: "networks_users", foreign_key: 'user_id', association_foreign_key: 'network_id'
...
end
現在,它起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.