如何通过 find_by_sql 使用 Ransack
How o use Ransack with find_by_sql
如何将 Ransack 与 "find_by_sql" 一起使用?
我通常这样做:
def index
@m = Mymodel.ransack(params[:q])
@mymodels = @m.result.page(params[:page])
end
我可以在进行自定义查询时使用 Ransack 吗?:
@mymodels = Mymodel.find_by_sql(["SELECT ... ", current_user])
如果我没记错的话,这就是你想要的:
def index
@m = Mymodel.ransack(params[:q])
@mymodels = @m.result.page(param[:page]).where(user: current_user)
end
虽然要直接回答您的问题,您可以将其转换为 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
如何将 Ransack 与 "find_by_sql" 一起使用? 我通常这样做:
def index
@m = Mymodel.ransack(params[:q])
@mymodels = @m.result.page(params[:page])
end
我可以在进行自定义查询时使用 Ransack 吗?:
@mymodels = Mymodel.find_by_sql(["SELECT ... ", current_user])
如果我没记错的话,这就是你想要的:
def index
@m = Mymodel.ransack(params[:q])
@mymodels = @m.result.page(param[:page]).where(user: current_user)
end
虽然要直接回答您的问题,您可以将其转换为 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