多对多关系仅在 Rails 中以一种方式工作

Many-to-many relationship only working one way in Rails

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

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

交付总监有客户总监有Pods有客户经理有公司。

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

整个结构都在工作,一直到公司,然后突然停止。交付总监 returns [] 上公司。

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 看起来像:

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 returns [].

如果有人能为我指出正确的方向,那就太好了。没有任何错误信息,似乎也没有任何问题。

哦,如果有帮助,这里是查询生成的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" =   [["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

您使用单一 Table 继承。您的 3 个模型:DeliveryDirectorAccountDirectorAccountManagerUser 模型的后代。当做浅层请求时它工作正常,但是当你构建涉及所有 3 个模型的请求时 Rails 无法构建正确的查询。如果你尝试设计如何在数据库方面找到一个交付主管的所有公司,你将得出 tables:

的链
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 table 名称。要过滤结果,它使用条件 "users"."type" IN ('AccountDirector') 这在您的情况下还不够,因为在您的查询中还应该有 AccountManager (作为 pods 和 [=23 之间的 link =]).

另一个迹象表明 Rails 很困惑:尽管模型中的关联正确 Rails 试图使用 table account_directors_companies 而你显然没有。

我建议检查您的数据库模式并将用户角色和它们之间的关系提取到单独的实体中。

更新:

例如,用户 authentication/registration 数据可以保留在 users table 中。所有关于用户角色及其关系的信息都可以移动到额外的 tables,由模型支持:

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

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

因此,要获取交付主管的公司,您可以致电:

DeliveryDirector.find_by(user_id: user_id).companies