简体   繁体   English

通过SQL LIKE通过多态关联进行Rails搜索

[英]Rails search by SQL LIKE through polymorphic association

I have a rails api, with the following models: 我有一个Rails API,具有以下模型:

Players::NflPlayer 玩家:: NflPlayer

class Players::NflPlayer < ApplicationRecord
  #################### Associations
  has_many :fantasy_players, class_name: "Leagues::FantasyPlayer", as: :player_entity, dependent: :destroy

 #################### Validations
  validates_presence_of :name

Players::TeamPlayer 玩家:: TeamPlayer

class Players::TeamPlayer < ApplicationRecord
  #################### Associations
  has_many :fantasy_players, class_name: "Leagues::FantasyPlayer", as: :player_entity, dependent: :destroy

  #################### Validations
  validates_presence_of :name

And Leagues::FantasyPlayer 和联盟::幻想玩家

class Leagues::FantasyPlayer < ApplicationRecord
  #################### Associations
  belongs_to :league, class_name: "Leagues::League"
  belongs_to :player_entity, polymorphic: true

In essence, a fantasy player can either be an NFL player, or a "Team" player, and that association is captured by the polymorphic variable "player_entity." 本质上,幻想玩家可以是NFL玩家,也可以是“ Team”玩家,并且该关联由多态变量“ player_entity”捕获。 Both the Players::NflPlayer and Players::TeamPlayer tables must have a column "name" Players :: NflPlayer和Players :: TeamPlayer表都必须具有“名称”列

I'm trying to a do a search for certain fantasy players, by name, where the search term refers to the "name" column of either Players::NflPlayer or Players::TeamPlayer tables. 我正在尝试通过名称搜索某些幻想玩家,其中搜索词指的是Players :: NflPlayer或Players :: TeamPlayer表的“名称”列。 This is the query I have so far to do this: 这是我到目前为止要做的查询:

Leagues::FantasyPlayer.where(
    league_id: league_id,
    player_entity: Players::NflPlayer.where("name LIKE :search_term", search_term: "#{search_term}%") || Players::TeamPlayer.where("name LIKE :search_term", search_term: "#{search_term}%")
  )

However, this only seems to load Leagues::FantasyPlayers that are associated with the PLayers::NflPlayer table, and not the Players::TeamPlayer table. 但是,这似乎只会加载与PLayers :: NflPlayer表关联的Leagues :: FantasyPlayers,而不是Players :: TeamPlayer表。

I think maybe I have to do a joins on those two tables first, but I'm not sure how to do that in this single query. 我想也许我必须先对这两个表进行联接,但是我不确定如何在单个查询中进行联接。

Any help would be much appreciated, thank you! 任何帮助将不胜感激,谢谢!

Unfortunately, the following line: 不幸的是,以下行:

Players::NflPlayer.where("name LIKE :search_term", search_term: "#{search_term}%") || Players::TeamPlayer.where("name LIKE :search_term", search_term: "#{search_term}%")

returns the first condition of the || 返回||的第一个条件 . As a result you receive: 结果,您收到:

Leagues::FantasyPlayer.where(
    league_id: league_id,
    player_entity: Players::NflPlayer.where("name LIKE :search_term", search_term: "#{search_term}%")
  )

In order to make it work, you need SQL's OR , Rails 5 supports it the following way: 为了使其工作,您需要SQL的OR ,Rails 5通过以下方式支持它:

Leagues::FantasyPlayer.where(
  league_id: league_id,
  player_entity: Players::NflPlayer.where("name LIKE :search_term", search_term: "#{search_term}%")
).or(
  Leagues::FantasyPlayer.where(
    league_id: league_id,
    player_entity: Players::TeamPlayer.where("name LIKE :search_term", search_term: "#{search_term}%")
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM