find all result when select category all in rails - ruby-on-rails

actually i have a problem as i want to search all the result when i select All from category.As now i have 4 options in my category. I have a query to find the result against three categories like tha,
This shows the result against 3 categories like againt home,apartment and plaza,But when i select all then nothing happen.What i should do that when i select all then all the properties will show.Thanks

Try something like that :
#properties = Property.includes(:rooms)
#properties = (params["search"] == 'ALL') ? #properties.all : #properties.where(params["search"])
#properties = #properties.paginate(:page => params[:page], :per_page => 8)
In my code, I suppose the value for 'All category' would be 'ALL'

Related

Sorting a result defined by remote parameters

i have this index action:
def index
limit = params[:limit]
page = params[:page]
sort = params[:sort].split(',')
#term = nil
if params[:search]
#term = params[:search]
#lessons = policy_scope(Lesson).search(#term)
.order("#{sort[0]} #{sort[1].upcase}")
.paginate(page: page, per_page: limit)
else
#lessons = policy_scope(Lesson).order("#{sort[0]} #{sort[1].upcase}")
.paginate(page: page, per_page: limit)
end
end
which is fed by a vuejs frontend with a vuetify datatable and its purpose is to send out an array of lesson objects, filtered and sorted by the frontend.
this works pretty good with default rails..
however, with the mobility gem involved there is no fieldname "title" for example or "title_en", so the order stops working. in mobility you have some "fake column names" and it automagically handles it to search for the value in a key-value table ( https://github.com/shioyama/mobility#getting-started )
so i sat me down and fired up the console and figured out that:
.order(title: :desc) - works
.order(title_en: :asc) - works
everything with strings involved, like .order('title DESC') or .order('title_en ASC') does not work and results in an error like
ActionView::Template::Error (PG::UndefinedColumn: ERROR: column
"title_en" does not exist LINE 1: SELECT "lessons".* FROM "lessons"
ORDER BY title_en ASC LIMI...
is there a way i could get this working? maybe there is something to generate title: out of 'title'? Or some other magic?
thanks!
You can call the order method like this instead:
.order(sort[0] => sort[1])
Passing "a" => "b" to a method is the same as passing "a": "b".

Will_Paginate - how to get an array of all paginated object ids?

I'm using will_paginate 2.3.x gem and I'm trying to get an array of all the ids of the objects that are in the WillPaginate::Collection. Is there a simple way to iterate all the items in the collection? With ActiveRecord collection this is simple:
all_ids = MyObject.find(:all).map { |o| o.id }
However, when paginated, the collection returns only the N elements that are in the first page:
not_all_ids = MyObject.paginate(:page => 1).map { |o| o.id }
What I want is to go through all the pages in the collection. Basically, I'm looking for a method that retrieves the next page. Any thoughts? thanks.
EDIT - I'm using rails 2.3. Also, I'm using pagination with some conditions but dropped them to simplify:
MyObject.paginate(:conditions => [...], :include => [...], :page => 1)
You could do something like that :
ids = MyObject.select("id").paginate(:page => 1)
if ids.total_pages > 1
(2..ids.total_pages).each do |page|
ids << MyObject.select("id").paginate(:page => page)
end
end
ids = ids.map(&:id)
But why using paginate in this case ?
ids = MyObject.select("id").map(&:id)
will be faster, and will use less resources ... if you're db contains 10000 elements and you're iterating 10 at a times you'll make 1000 cals to your db vs 1 :-)
ps: I'm using .select("id") so the request generated is :
SELECT id from users;
VS
SELECT * FROM users;
I'm also using a nice shortcut map(&:id), this is the equivalent of .map { |o| o.id }

Search with multiple columns in Rails

While using the command:
#items = Item.find(:all,:order => 'name', :conditions => ["name LIKE ?", "%#{params[:key]}%"])
This works perfectly fine, but the search is only based on just a column. How do i make it search other columns, like description, category?
I am assuming you are using Rails 2, since you are using ActiveRecord 2 style syntax. You can just add the other columns as additional conditions, like this:
key = "%#{params[:key]}%"
#items = Item.find(:all, :conditions => [
'name LIKE ? OR description LIKE ? OR category LIKE ?',
key, key, key
], :order => 'name')
For reference, here's how you would do it in Rails 3:
key = "%#{params[:key]}%"
#items = Item.where('name LIKE ? OR description LIKE ? OR category LIKE ?', key, key, key).order(:name)
What if you have 15 columns to search then you will repeat key 15 times. Instead of repeating key 15 times in query you can write like this:
key = "%#{params[:key]}%"
#items = Item.where('name LIKE :search OR description LIKE :search OR category LIKE :search', search: key).order(:name)
It will give you same result.
Thanks

Rails Will Paginate problem

I´m paginating with the will_paginate plugin.
I have a popup that adds #products when the user clicks the button Add via an Ajax request.
Note: The #products is paginated
In Ajax Request I update the product-list :
ids = params[:accessories][:id].join(" and id <> ")
#products = Product.find_by_sql("select * from products where id <> #{ids}")
However if I use: #products = Product.find_by_sql("select * from products where id <> #{ids}").paginate
it returns identical array.
So how I could paginate the array again?
Why are you using find_by_sql method?
May be you can write something like this:
ids = params[:accessories][:id].join(",")
#products = Product.paginate(:page => params[:page], :conditions => ["not (id in (?))", ids])
also conditions you can move to product model and replace it with scope(Rails 3) or named_scope(Rails 2).
Already tried:
#products = Product.paginate :conditions => conditions

performing a search on a form - search in the filtered list of entries displayed in view

i really need some help on this. I searched the internet but couldn't find a single solution to my problem.
I have got an index.html.erb file which displays some records which have been retrieved using a rather complex find_by_sql. Please see code below:
def index
#refprobes = Refprobe.paginate_by_sql ["select * from ref_probe
where RPR_OID in
(SELECT DISTINCT RPR_OID
FROM REF_PROBE
JOIN ISH_PROBE ON RPR_OID = PRB_MAPROBE
JOIN ISH_SUBMISSION ON PRB_SUBMISSION_FK = SUB_OID
JOIN SEC_USER_PI ON USP_PI_ID = SUB_PI_FK
WHERE USP_USER_FK = " + session[:user_id]+ ")"], :page => params[:page], :per_page => 10
end
Now i want to include a search field on top of my index.html.erb, which will allow user to filter those displayed entries based on the value inputted in the search field.
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
The above code does not fit my requirements as i don't want to search through all the records in the table. I want to restrict my search to only those entries displayed my index.html.erb.
How can i do this?
Many many thanks in advance for your help..
read about scopes

Resources