多对多关系仅在Rails中起作用

时间:2018-10-23 08:51:32

标签: ruby-on-rails activerecord relational-database

我不知道是否有人可以帮助我,因为这有点奇怪。

我在数据库中有一组相当复杂的关系,其结构大致如下:

送货主任有客户总监有豆荚有客户经理有公司

因此,交付总监应该拥有公司。

整个结构一直有效,一直到公司,然后突然停止。送货总监向公司退还[]。

class DeliveryDirector < User
  has_many :account_directors
  has_many :pods, through: :account_directors
  has_many :account_managers, through: :pods
  has_many :companies, through: :account_managers
end

公司类如下:

class Company < ApplicationRecord
   belongs_to :account_manager
   has_one :pod, through: :account_manager
   has_one :account_director, through: :pod
   has_one :delivery_director, through: :account_manager
end

就像我说的,一切正常。公司甚至还有送货主任!只是DeliveryDirector.all.first.companies返回[]。

如果有人甚至可以将我指向正确的方向,那就太好了。没有错误消息,似乎也没有任何问题。

哦,如果有帮助的话,这是查询生成的SQL:

Company Load (0.7ms)  SELECT "companies".* FROM "companies" INNER JOIN "users" ON "companies"."account_manager_id" = "users"."id" INNER JOIN "pods" ON "users"."pod_id" = "pods"."id" INNER JOIN "users" "account_directors_companies" ON "pods"."account_director_id" = "account_directors_companies"."id" WHERE "users"."type" IN ('AccountDirector') AND "account_directors_companies"."delivery_director_id" = $1  [["delivery_director_id", 2]]

谢谢!

编辑:请求其他模型,模式

吊舱:

class Pod < ApplicationRecord
   belongs_to :account_director
   has_many :account_managers
   has_many :companies, through: :account_managers
end

客户经理:

class AccountManager < User
  belongs_to :pod
  has_one :account_director, through: :pod
  has_one :delivery_director, through: :account_director
  has_many :companies
end

模式:

ActiveRecord::Schema.define(version: 2018_10_19_141416) do


  enable_extension "plpgsql"

  create_table "companies", force: :cascade do |t|
    t.string "name"
    t.string "officelocation"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "campaign_link"
    t.string "company_logo"
    t.string "website"
    t.integer "account_manager_id"
  end

  create_table "images", force: :cascade do |t|
    t.string "name"
    t.string "location"
    t.bigint "company_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["company_id"], name: "index_images_on_company_id"
  end

  create_table "jwt_blacklist", id: :serial, force: :cascade do |t|
    t.string "jti", null: false
    t.index ["jti"], name: "index_jwt_blacklist_on_jti"
  end

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

  create_table "markets_users", id: false, force: :cascade do |t|
    t.bigint "market_id", null: false
    t.bigint "talent_manager_id", null: false
  end

  create_table "pods", force: :cascade do |t|
    t.string "name"
    t.integer "account_director_id"
    t.integer "delivery_director_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "table_campaigns", force: :cascade do |t|
    t.bigint "user_id"
    t.bigint "company_id"
    t.string "name"
    t.integer "iterations"
    t.integer "interviews"
    t.index ["company_id"], name: 
    "index_table_campaigns_on_company_id"
    t.index ["user_id"], name: "index_table_campaigns_on_user_id"
 end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "jobtitle"
    t.string "linkedin"
    t.string "office"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "type"
    t.integer "team_lead_id"
    t.integer "delivery_director_id"
    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.inet "current_sign_in_ip"
    t.inet "last_sign_in_ip"
    t.bigint "pod_id"
    t.string "user_photo"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["pod_id"], name: "index_users_on_pod_id"
    t.index ["reset_password_token"], name: 
     "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "table_campaigns", "companies"
  add_foreign_key "table_campaigns", "users"
end

现在添加客户总监:

class AccountDirector < User
  belongs_to :delivery_director
  has_one :pod
  has_many :account_managers, through: :pod
  has_many :companies, through: :account_managers
end

1 个答案:

答案 0 :(得分:2)

您使用单表继承。您的模型中的3种:DeliveryDirectorAccountDirectorAccountManagerUser模型的后代。当执行浅层请求时,它可以正常工作,但是当您构造涉及所有3个模型的请求时,Rails无法构建正确的查询。如果您尝试设计如何从数据库中查找交货主管的所有公司,那么您将进入表格链:

companies -> users (account managers) -> pods -> users (account directors) -> users (delivery directors)

您的请求的SQL查询可能类似于:

SELECT companies.* FROM companies
  INNER JOIN users AS account_managers ON companies.account_manager_id = account_managers.id
  INNER JOIN pods ON account_managers.pod_id = pods.id
  INNER JOIN users AS account_directors ON pods.account_director_id = account_directors.id
  INNER JOIN users AS delivery_directors ON account_directors.delivery_director_id = delivery_directors.id
WHERE delivery_directors.id = 2;

但是很明显,Rails不会在查询中添加AS子句来区分用户角色,而是使用users表名。要过滤结果,它使用条件"users"."type" IN ('AccountDirector'),这在您的情况下还不够,因为在您的查询中还应该有AccountManager(作为podscompanies之间的链接)。

Rails感到困惑的另一个迹象:尽管模型中的关联正确,但是Rails仍尝试使用表account_directors_companies,而您显然没有。

我建议检查您的数据库架构,并将用户角色及其之间的关系提取到单独的物质中。

更新:

例如,用户身份验证/注册数据可以像现在一样保留在users表中。有关用户角色及其关系的所有信息都可以移动到额外的表中,并由模型进行备份:

class DeliveryDirector < ApplicationRecord
  belongs_to :user
  has_many :account_directors
  has_many :pods, through: :account_directors
  has_many :account_managers, through: :pods
  has_many :companies, through: :account_managers
end

class AccountDirector < ApplicationRecord
  belongs_to :user
  has_one :pod
  has_many :account_managers, through: :pod
  has_many :companies, through: :account_managers
end

class AccountManager < ApplicationRecord
  belongs_to :user
  has_many :companies
end

这些模型中的每一个在数据库中都有自己的表。

因此,要获取送货总监公司,您可以致电:

DeliveryDirector.find_by(user_id: user_id).companies