ActiveRecord Как объединить два условия на где с объединением - PullRequest
0 голосов
/ 26 апреля 2020

У меня есть две таблицы с таблицей соединений между ними:

Серия

  create_table "series", force: :cascade do |t|
    t.string "title", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  class Series < ApplicationRecord
    include PgSearch::Model

    has_many :makers
    has_many :creators, through: :makers
    has_many :sources, dependent: :destroy
    has_many :entries, through: :sources

    validates :title, presence: true

    pg_search_scope :search_by_title, against: {
      title: 'A',
      title_en: 'B',
      title_en_jp: 'C'
    }
  end

Создатель

  create_table "makers", force: :cascade do |t|
    t.bigint "series_id", null: false
    t.bigint "creator_id", null: false
    t.bigint "creator_type", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["creator_id"], name: "index_makers_on_creator_id"
    t.index ["series_id", "creator_id", "creator_type"], name: "index_makers_on_series_and_creator_and_type", unique: true
    t.index ["series_id"], name: "index_makers_on_series_id"
  end

  # frozen_string_literal: true

  class Maker < ApplicationRecord
    extend Enumerize

    belongs_to :series
    belongs_to :creator

    validates :series, uniqueness: { scope: %i[creator creator_type] }

    enumerize :creator_type, in: {
      author: 1,
      artist: 2
    }, predicates: true, scope: true
  end

Создатель

  create_table "creators", force: :cascade do |t|
    t.string "name", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["name"], name: "index_creators_on_name"
  end

  class Creator < ApplicationRecord
    include PgSearch::Model

    has_many :makers
    has_many :series, through: :makers

    pg_search_scope :search_by_title, against: :name
  end

Основываясь на таблицах выше, я хотел создать метод where, который бы нашел Series для конкретных c производителей. Проблема в сериях, в которых есть два создателя, обычно author и artist.

Так что пока этот код работает, найти Series для указанного c создателя:

  def self.find_by_creators(title, creators)
    where(title: title)
      .joins(makers: :creator)
      .where(
        makers: {
          creator_type: :author,
          creators: { name: creators[:authors] }
        }
      )
  end

при попытке добавить еще where ничего не возвращается:

  def self.find_by_creators(title, creators)
    where(title: title)
      .joins(makers: :creator)
      .where(
        makers: {
          creator_type: :author,
          creators: { name: creators[:authors] }
        }
      )
      .where(
        makers: {
          creator_type: :artist,
          creators: { name: creators[:artists] }
        }
      )
  end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...