2017-10-10 25 views
0

「find_by_sql」でRansackを使用するにはどうすればよいですか?find_by_sqlでRansackを使用する方法

def index 
    @m = Mymodel.ransack(params[:q]) 
    @mymodels = @m.result.page(param[:page]).where(user: current_user) 
end 

:私はそれを正しく取得する場合、これはあなたが欲しい欲しいですカスタムクエリを実行する際

def index 

    @m = Mymodel.ransack(params[:q]) 
    @mymodels = @m.result.page(params[:page]) 

end 

私は掻き回すを使用することはできます?:

@mymodels = Mymodel.find_by_sql(["SELECT ... ", current_user]) 
+0

スコープを使用している可能性があります。https://github.com/activerecord-hackery/ransack#using-scopesclass-methods – vzamanillo

答えて

0

: は、私は一般的にこれを行いますあなたの質問に直接答えるには、それをSQL文字列に変換できますが、current_userを引数として使用することはできません(または難しくなります):

def index 
    @m = Mymodel.ransack(params[:q]) 
    sql = @m.result.page(param[:page]).to_sql 
    @mymodels = Mymodel.find_by_sql(sql, current_user) 
    # the line just above won't work immediately because you'll need to 
    # modify the `sql` variable manually by inserting the `?` that you'll 
    # need to map that to the `current_user` that you passed as an argument 
end 
関連する問題