Ruby on rails 如何使用Ransack和find_by_sql

Ruby on rails 如何使用Ransack和find_by_sql,ruby-on-rails,ransack,Ruby On Rails,Ransack,如何使用Ransack和“按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 = M

如何使用Ransack和“按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\u 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

如果我理解正确,这就是你想要的:

def index
  @m = Mymodel.ransack(params[:q])
  @mymodels = @m.result.page(param[:page]).where(user: current_user)
end
要直接回答您的问题,您可以将其转换为SQL字符串,但您不能(或很难)使用
current\u 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
也许用一个范围,也许用一个范围,