2017-12-01 7 views
0

railsアプリケーションで簡単な検索を実装しようとしています。検索クエリが入力を受け付けていない - ActiveRecord enum LIKE

productions_controller

def filter 
    if params[:filter] 
     @productions = Production.where('productions.status like ?', "%#{Production.statuses[params[:filter]]}%") 
    else 
     @productions = Production.all 
    end 
end 

インデックスページ上のレコードのリストがあります。私はenumデータ型であるstatusに基づいてそれらのリストの検索/フィルタを実装しています。私は、テキストフィールドにキーワードpreに入ったとき

<%= form_tag [:filter, :productions], :method => 'get' do %> 
    <p> 
    <%= text_field_tag :filter, params[:filter] %> 
    <%= submit_tag "Filter", :status => nil %> 
    </p> 
<% end %> 

index.html.erbが、これは、それが入力を取っているように見えます

Processing by ProductionsController#filter as HTML 
    Parameters: {"utf8"=>"✓", "filter"=>"pre", "commit"=>"Filter"} 
    Rendering productions/filter.html.erb within layouts/application 
    Production Load (0.4ms) SELECT "productions".* FROM "productions" WHERE (productions.status like '%%') 
    Rendered productions/filter.html.erb 

ログに何が起こるかでありますキーワードを使用していますたぶん私は間違っています。誰かが私を助けてくれましたか?

要求

class Production < ApplicationRecord  
    enum status:{ 
    Preproduction:1, 
    Postproduction: 2, 
    Completed:3 
    } 
end 
+0

「Production.statuses」とは何ですか? 'Production'モデルコードを投稿できますか? –

+0

@ GokulM数字の代わりに割り当てられた文字列名に基づいてステータスを照会するには、それを使用しなければなりませんでした。モデルを追加しました – user3576036

+0

Production.statuses => {"Preproduction" => 1、 "Postproduction" => 2、 "Completed" => 3} – user3576036

答えて

0

としてproduction.rbを追加するには、ちょうどあなたは、あなたがやっているようLIKE文で列挙型を使用することはできません、実際にはこれらの

def filter 
    if params[:filter] 
     @productions = Production.where('productions.status like ?', "%#{params[:filter]}%") 
    else 
     @productions = Production.all 
    end 
end 
+0

これを行うと、 '1'や' 2'のようなテキストフィールドです。しかし、私はproduction.rbモデルで 'Preproduction'のような名前を付けたいと思っています。 – user3576036

1

のようなだけparams[:filter] を渡します。あなたがこのように質問した場合にのみ動作します。ここで

@productions = Production.where(status: "Preproduction") 

は、同様の問題を持つ別のポストです:
rails 5 enum where "like"

私はあなたがステータスとしてこの

<%= form_tag [:filter, :productions], :method => 'get' do %> 
    <p> 
    <%= select_tag :filter, options_for_select(Production.statuses) %> 
    <%= submit_tag "Filter", :status => nil %> 
    </p> 
<% end %> 
+0

編集@ankit patidarを気に入ってください! – Code860

1

<%= form_tag [:filter, :productions], :method => 'get' do %> 
    <p> 
    <%= text_field_tag :filter, params[:filter] %> 
    <%= submit_tag "Filter", :status => nil %> 
    </p> 
<% end %> 

を変更する推薦として格納されていますあなたは以下のメソッドをfiに使うことができますステータス値に基づいてproductionsを受け取ります。

def filter 
    filtered_statuses = params[:filter].present? ? Production.statuses.select{|k, v| k.to_s.include?(params[:filter])} : nil 

    if filtered_statuses 
     @productions = Production.where(status: filtered_statuses.values) 
    elsif params[:filter].present? 
     @productions = [] # To return empty if no search filter matches production statuses 
    else 
     @productions = Production.all 
    end 
end 
0

あなたはフィルタとしてpreを渡しているとあなたは前を持つ任意のステータスを持っていない、それはあなたが任意のデータを見つけることができないのと同じ理由です。
あなたはまだ、フィルタ単語がすでに文字列補間を行っているとして、それはなりますので、それはあなたにnilを与える任意のキーと一致していないかのようにビット

 @productions = Production.where('productions.status like ?', "%#{Production.statuses[params[:filter].capitalize]}%") 

問題は、コードではなく、コードを更新する必要がありますが空白で、最後にあなたのenumが機能していないように見える"%%"にアクセスしようとしています。 enumのフィルタを提供する場合は、入力の代わりにselect filedを指定し、答えに記載されているようにステータスに基づいてデータを直接フィルタリングします。
希望します。

+0

あなたのソリューションも機能します。うん、私は完全なキーワードを入力するはずです。現在、テキストフィールドを使用してキーワードを入力しています。状態のキーワードは固定されているので、ドロップダウンに表示されて選択する方法はありますか?それ、どうやったら出来るの ? – user3576036

+0

'select_tag(:status、options_for_select(Production.statuses.map {| h、k | [h、k]}))'または 'options_for_select(Production.statuses)'これはうまくいくかもしれません。 – Manishh

関連する問題