活动记录通过两种模型has_many



我有以下上下文:

4 型号:

  • 项目
  • 投资者
  • 订阅
  • 外部订阅

project应通过subscriptionsexternal_subscriptions进行多次investors

我目前有一种方法可以执行以下操作:Investor.where(id: (subscription_ids + external_subscription_ids)).

我的目标是建立has_many关系(并精确地使用 has_many 活动记录功能(以获得相同的结果。我怎样才能做到这一点?甚至可能吗?

谢谢!

Project
[Associations]
  has_many :subscriptions
  has_many :external_subscriptions
[Table description]
  create_table "projects", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Investor
[Associations]
  has_many :subscriptions
  has_many :external_subscriptions
[Table description]
  create_table "investors", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Subscription
[Associations]
  belongs_to :project
  belongs_to :investor
[Table description]
  create_table "subscriptions", force: :cascade do |t|
    t.integer "project_id"
    t.integer "investor_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["investor_id"], name: "index_subscriptions_on_investor_id"
    t.index ["project_id"], name: "index_subscriptions_on_project_id"
  end

ExternalSubscription
[Associations]
  belongs_to :project
  belongs_to :investor
[Table description]
  create_table "external_subscriptions", force: :cascade do |t|
    t.integer "project_id"
    t.integer "investor_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["investor_id"], name: "index_external_subscriptions_on_investor_id"
    t.index ["project_id"], name: "index_external_subscriptions_on_project_id"
  end

我在轨道上 5.0.x

编辑

我的真实模型比这更复杂。在这里,我只是展示关系以使其易于讨论,但我无法将subscriptionsexternal_subscriptions合并到同一模型中。

由于您的订阅和external_subscriptions中似乎不需要不同的信息(两个表具有相同的字段(,我将只使用一个模型和表,并根据表中的新字段对订阅进行分类。通过使用适当的范围,您可以轻松访问所有关联的模型。

项目

class Project < ApplicationRecord
  has_many :subscriptions
  has_many :external_subscriptions, -> { external }, class_name: "Subscription"
  has_many :normal_subscriptions, -> { normal }, class_name: "Subscription"
  has_many :investors, through: :subscriptions
  has_many :external_investors, through: :external_subscriptions, :source => :investor
  has_many :normal_investors, through: :normal_subscriptions, :source => :investor
end

投资者

class Investor < ApplicationRecord
  has_many :subscriptions
  has_many :projects, through: : subscriptions
end

订阅

class Subscription < ApplicationRecord
  belongs_to :project
  belongs_to :investor
  enum type: [ :external, :normal ]
  scope :external, -> { where(type: :external) }
  scope :normal, -> { where(type: :normal) } 
end

然后,您可以通过以下方式访问不同的项目投资者:

project = Project.first.
project.investors #all
project.external_investors #only external
project.normal_investors #only normal
由于@MrYoshiji的评论

,我解决了SQL视图执行UNION的问题。

这是 POC:https://github.com/yoones/rails-has_many-through-view

最新更新