2017-10-27 15 views
0

同じ検索を使用してカテゴリ名検​​索用のhas_many(カテゴリ)テーブル内のレコードを検索すると、別の列からレコードを検索できる検索が実装されました。フィールド。OR検索条件付きの検索フィールドに基づいたレコードの検索方法

私が持っているこのようなモデル:

インフルエンサー:

has_many :influencer_categories, dependent: :destroy 
has_many :categories, through: :influencer_categories 

コードは次のとおりです。

コントローラー:

@influencers = Influencer.search(params) 

モデル:

def self.search(params) 
     .includes(:categories) 
     .where(search_all(params[:q])) 
     .where("LOWER(categories.name) like '%#{params[:q].downcase}%'") 
end 

def self.search_all(criteria) 
    if criteria.include?('@') && criteria.start_with?('@') 
     criteria.split(' ').map {|criterion| "LOWER(username) like '%#{criterion.downcase.gsub('@', '')}%'"}.join(' OR ') 
    elsif criteria.include?('#') && criteria.start_with?('#') 
     criteria.split(' ').map {|criterion| "LOWER(bio) like '%#{criterion.downcase.gsub('#', '')}%'"}.join(' OR ') 
    else 
     "LOWER(full_name) like '%#{criteria.downcase}%' OR LOWER(username) like '%#{criteria.downcase}%' OR LOWER(bio) like '%#{criteria.downcase}%'" 
    end 
    end 

出力誤差がある:

ActionView::Template::Error (PG::UndefinedTable: ERROR: missing FROM-clause 
entry for table "categories" 
LINE 1: ... '%food%' OR LOWER(bio) like '%food%') AND (LOWER(categories... 

                  ^
: SELECT COUNT(*) FROM "influencers" WHERE (LOWER(full_name) like '%food%' OR 
LOWER(username) like '%food%' OR LOWER(bio) like '%food%') AND 
(LOWER(categories.name) like '%food%')): 

おそらく、私は食品カテゴリを持つすべてのインフルエンサー現れ、食品カテゴリの所望の結果を得るために、私の状態でORを含める必要があります。 私は何をする必要があります。

私はこのような何かがうまくいくと信じて:

SELECT "influencers"."id" FROM "influencers" LEFT OUTER JOIN 
"influencer_categories" ON "influencer_categories"."influencer_id" = 
"influencers"."id" LEFT OUTER JOIN "categories" ON "categories"."id" = 
"influencer_categories"."category_id" WHERE (LOWER(full_name) like '%food%' OR 
LOWER(username) like '%food%' OR LOWER(bio) like '%food%' OR 
LOWER(categories.name) like '%food%') 

答えて

0

最後に、以下のクエリは私と一緒によく働い:

私はhas_manyの先端の一部またはクエリの私の最初の部分を連結するために必要な。

また、エラーを避けるためにreferencesも必要です。次のSQLクエリを出力

def self.search(params) 
    .includes(:categories) 
    .where(search_all(params[:q])+" OR LOWER(categories.name) like 
'%#{params[:q].downcase}%'") 
    .references(:categories) 
end 

:それは、よりエレガントな方法で行うことができれば

SELECT "influencers"."id" AS t0_r0, "influencers"."instagram_id" AS t0_r1, 
"influencers"."full_name" AS t0_r2, "influencers"."username" AS t0_r3, 
"influencers"."profile_picture" AS t0_r4, "influencers"."website" AS t0_r5, 
"influencers"."bio" AS t0_r6, "influencers"."created_at" AS t0_r7, 
"influencers"."updated_at" AS t0_r8, "influencers"."slug" AS t0_r9, 
"influencers"."email" AS t0_r10, "influencers"."follower_count" AS t0_r11, 
"influencers"."like_count" AS t0_r12, "influencers"."comment_count" AS t0_r13, 
"influencers"."media_count" AS t0_r14, "influencers"."country" AS t0_r15, 
"influencers"."state" AS t0_r16, "influencers"."city" AS t0_r17, 
"categories"."id" AS t1_r0, "categories"."name" AS t1_r1, 
"categories"."created_at" AS t1_r2, "categories"."updated_at" AS t1_r3 FROM 
"influencers" LEFT OUTER JOIN "influencer_categories" ON 
"influencer_categories"."influencer_id" = "influencers"."id" LEFT OUTER JOIN 
"categories" ON "categories"."id" = "influencer_categories"."category_id" 
WHERE (LOWER(full_name) like '%food%' OR LOWER(username) like '%food%' OR 
LOWER(bio) like '%food%' OR LOWER(categories.name) like '%food%') AND 
"influencers"."id" IN (188, 189) 

は、あなたの答えやコメントを追加してください。

関連する問題