How o use Ransack with find_by_sql - ruby-on-rails

How can I use Ransack with "find_by_sql"?
I generally do this:
def index
#m = Mymodel.ransack(params[:q])
#mymodels = #m.result.page(params[:page])
end
Can I use Ransack when doing a custom query?:
#mymodels = Mymodel.find_by_sql(["SELECT ... ", current_user])

If I get it correctly, this is want you want:
def index
#m = Mymodel.ransack(params[:q])
#mymodels = #m.result.page(param[:page]).where(user: current_user)
end
To answer your question directly though, you can convert it to an SQL string, but you won't (or would be hard) be able to use current_user as an argument:
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

Related

How to query records with and without params in Rails?

I have model Places and I have the index method in a controller. I need to get all places via request
/places
And filter places via request with query
/places?tlat=xxxx&tlong=xxxx&blat=xxxxx&blong=xxxx
What the best way to get this records? Should I check an existence of each param or are there Rails way?
#places = if params[tlat]&&params[blat]....
Places.all.where("lat > ? AND long > ? AND lat < ? AND long < ?", tlat, tlong, blat, blong)
else
Places.all
If you want to set WHERE clauses depending on params, you can use Ursus' code which is fine.
However, if you need to apply those WHERE clauses only if a set of params are present, you can use the following:
#places = Place.all
if params[:blat].present? && params[:tlat].present?
#places = #places.where(blat: params[:blat], tlat: params[:tlat])
end
# etc.
You could use an array of arrays to pair the associated params, kind of like what Ursus did.
I'd do something like this if possible. Important to note the this is just one query, composed dynamically.
#places = Place.all
%i(tlat tlong blat blong).each do |field|
if params[field].present?
#places = #places.where(field => params[field])
end
end
IMO, truly the "Rails way" (but actually just the "Ruby way") would be to extract this long conditional, and the query itself, out to their own private method. It becomes much easier to understand what's going on in the index action
class MyController < ApplicationController
def index
#places = Place.all
apply_geo_scope if geo_params_present?
end
private
def geo_params_present?
!!(params[:tlat] && params[:blat] && params[:tlong] && params[:blong])
end
# A scope in the model would be better than defining this in the controller
def apply_geo_scope
%i(tlat tlong blat blong).each do |field|
#places = #places.where(field => params[field])
end
end
end

Passing multiple scopes to Concern Method - Ruby on Rails

In the process of drying my Rails app code, I have created the following concern that is used to generate the contents of an index method.
define_method(:generate_index) do |string, scope|
instance_variable_set( "##{string}", string.camelize.constantize.public_send(scope))
end
I use this code to generate something like the following:
def index
generate_index("foo", "all")
# #foo = Foo.all
end
What I'd like to do is to have the define method accept a number of scopes. I tried passing in an array of scopes but that results in an error.
Any ideas?
Thanks
You could use the splash * operator:
define_method(:generate_index) do |klass, *scopes|
scope = klass.to_s.camelize.constantize
scopes.each { |s| scope = scope.send(s) }
instance_variable_set("##{string}", scope)
end
def index
generate_index(:foo, :all, :where_not_test)
# #foo = Foo.all.where_not_test
end

Conditionally chaining where clauses in Rails ActiveRecord queries

I have a form that when filled has to trigger a particular query, depending on which parameters the form has, so I have a method in my model that I believe should look like this:
def form_query(params)
query = ''
if params.has_key?('size')
query = query.where(size: params['size'])
end
if params.has_key?('title')
query = query.where(title: params['title'])
end
# More conditionals depending on params.
end
My question is, what does query have to be at the beginning? I put query = '', but I am wondering what has to be the base case, so I can conditionally add more 'where' clauses.
Queries aren't strings; they're query objects. So you want something like
query = YourModel.scoped # Rails 3; in Rails 4, use .all
if params.has_key?('size')
query = query.where(size: params['size'])
end
etc.
Alternatively, you can update your code as below:
def self.form_query(params)
options = {}
fields = ["body", "title"].freeze ## Add other options
if params.present?
fields.each do |field|
options[field] = params[field] if params[field]
end
end
if options.present?
where(options)
else
all ## or nil if you don't want to show any records in view
end
end
Also, form_query should be a class method in your model.
Add more options in the fields array that you would like to query against.
It not only makes your code compact but also makes a single database call.
Here is a more condensed version of Kirti Thorat's version:
FIELDS = ["size", "title"].freeze ## Add other options
def self.form_query(params)
return all unless params.present?
options = params.select { |k, _v| FIELDS.include? k.to_s }
options.present? ? where(options) : all
end
I have done k.to_s so you can pass params keys as either strings or symbols.
If you want to return nil if no params are passed you can do this:
FIELDS = ["size", "title"].freeze ## Add other options
def self.form_query(params)
return unless params.present?
options = params.select { |k, _v| FIELDS.include? k.to_s }
where(options) if options.present?
end

How to handle additional param in controller

I'm learning Rails and I have now a good knowledge about controllers.
By the way I always have some issues and I don't know whats the best way to solve them.
One of these is the search: I have a search in my website and I must reorder results for relevance and date.
My search controller
def show
#query = params[:query]
#contents = Content.published.search_by_text(#query).page(params[:page]).per(12)
end
This is the default search. I have to implement also the "data order" search and I think to do something like this:
def show
#query = params[:query]
#contents = Content.published.search_by_text(#query).page(params[:page]).per(12)
if params[:order]
#contents = Content.published.search_by_text(#query).reorder("created_at DESC").page(params[:page]).per(12)
end
end
Is there a better way to obtain my result?
Fortunately, rails allows us to chain calls when using Active Record (Rails' ORM)
Here is a possibility :
def show
#query = params[:query]
#contents = Content.published.search_by_text(#query)
#contents = #contents.reorder("created_at DESC") if params[:order]
#contents = #contents.page(params[:page]).per(12)
end

Rails 3: Search method returns all models instead of specified

What I'm trying to do: I have a model "Recipe" in which I defined a method "search" that takes an array of strings from checkboxes (I call them tags), and a single string. The idea is to search the db for recipes that has anything in it's 'name' or 'instructions' that contains the string, AND also has any of the tags matching it's 'tags' property.
Problem: The search method return all the recipes in my db, and doesn't seem to work at all at finding by the specific parameters.
The action method in the controller:
def index
#recipes = Recipe.search(params[:search], params[:tag])
if !#recipes
#recipes = Recipe.all
end
respond_to do |format|
format.html
format.json { render json: #recipe }
end
end
The search method in my model:
def self.search(search, tags)
conditions = ""
search.present? do
# Condition 1: recipe.name OR instruction same as search?
conditions = "name LIKE ? OR instructions LIKE ?, '%#{search[0].strip}%', '%#{search[0].strip}%'"
# Condition 2: if tags included, any matching?
if !tags.empty?
tags.each do |tag|
conditions += "'AND tags LIKE ?', '%#{tag}%'"
end
end
end
# Hämtar och returnerar alla recipes där codition 1 och/eller 2 stämmer.
Recipe.find(:all, :conditions => [conditions]) unless conditions.length < 1
end
Any ideas why it return all records?
if you are using rails 3, then it is easy to chain find conditions
def self.search(string, tags)
klass = scoped
if string.present?
klass = klass.where('name LIKE ? OR instructions LIKE ?', "%#{string}%", "%#{string}%")
end
if tags.present?
tags.each do |tag|
klass = klass.where('tags LIKE ?', "%#{tag}%")
end
end
klass
end
When you do
search.present? do
...
end
The contents of that block are ignored - it's perfectly legal to pass a block to a function that doesn't expect one, however the block won't get called unless the functions decides to. As a result, none of your condition building code is executed. You probably meant
if search.present?
...
end
As jvnill points out, it is in general much nicer (and safer) to manipulate scopes than to build up SQL fragments by hand

Resources