Is there a way to add a "condition" to a ransack search? I want to search through published Articles only.
Currently my code looks like this and is working perfectly. But it shows articles that are not yet published, too.
#q = Article.ransack(params[:q])
#q.sorts = 'name asc' if #q.sorts.empty?
#article = #q.result(distinct: true).includes(:vendor,
:description,
:alt_art_names,
:alt_vendor_names,
:certificates,
:article_reports,
:article_ratings,
:article_community_ratings)
Is there a way to add something like "where('article.published_at <= ?', Time.now)"?
Try doing:
#q = Article.where("articles.published_at <= ?", Time.now).ransack(params[:q])
...
Related
I am using Ransack for Filtering and will_paginate for Pagination and it seems I have a problem combining both.
Analyzing memory problems of my app I saw that my Category controller was loading all entries from the database at each call.
def index
#q = Category.all_cached.paginate(:page => params[:page], :per_page => 30).ransack(params[:q])
#categories = #q.result(distinct: true)
end
In order to get the only the first entries for each page I tried the following change:
def index
#q = Category.paginate(:page => params[:page], :per_page => 30).ransack(params[:q])
#categories = #q.result(distinct: true)
end
Problem is that now the pagination is not working any longer.
Error says: Couldn't find Category with 'id'=x even though enough records are available.
I have two questions:
How can I avoid loading all entries at first call but instead per
page
As I also retrieve information about different products in
the category and I want to avoid n+1 calls, how can I add this
[includes(:product)] into the Category Controller?
Try this way:
def index
#q = Category.paginate(page: params[:page], per_page: 30).ransack(params[:q])
#categories = #q.result(distinct: true).includes(:products)
end
Ive been using a simple search method to implement a search feature for several parts of an app im using. The methods and controller index actions look like this.
Index action ( participants_controller.rb )
def index
if params[:search]
#participants = Participant.search(params[:search])
else
#participants = Participant.all
end
end
Participant Model ( participant.rb )
def self.search(terms)
if terms
where("email ILIKE ?", "%#{terms}%")
end
end
The issue im having is that I need to not only search by email ( which is how the self.search method is set up now. ) But I also need to search by first and last name ( f_name, l_name ). but this info is in an associated model called ParticipantDetail.
Ive tried the following but I get a 'no method participant_detail' error
def index
if params[:search]
#participants = Participant.participant_detail.search(params[:search])
else
#participants = Participant.all
end
end
Ive also tried searching like this...
def index
if params[:search]
#participants = ParticipantDetail.search(params[:search])
else
#participants = Participant.all
end
end
But this is actually no good because it only brings up the associated record and not the belonged_to (parent ) record.
any help would be greatly appreciated.
Assuming ParticipantDetail belongs_to :participant, and Participant has_one :participant_detail, use an INNER JOIN
Participant
.select('participants.*')
.select('participant_details.f_name, participant_details.l_name')
.joins(:participant_details)
.where('participants.email ILIKE :search OR participant_details.f_name ILIKE :search OR participant_details.l_name ILIKE :search', search: terms)
In your search method you can add the following:
joins(:participant_detail).where("participant_detail.f_name ILIKE :search OR participant_detail.l_name ILIKE :search OR email ILIKE :search", :search => "%#{terms}%")
and use #participants = Participant.search(params[:search]) to call it.
I'm new at the rails world, i am trying to implement will_paginate with my Search model. The Search model is responsible for searching products.
My problem is that i can't figure out how to make will_paginate works with this two models, because the products are displayed by the search model and will_paginate only deals with controllers, i have spent a couple of days trying to make this work but without luck :(
This is my Search.rb
class Search < ActiveRecord::Base
def search_products
products = Product.all
products = products.where(["modelo LIKE ?","%#{modelo}%"]).order(created_at: :desc) if modelo.present?
products = products.where(["preco >= ?",min_price]) if min_price.present?
products = products.where(["preco <= ?",max_price]) if max_price.present?
products = products.where(["troca LIKE ?","%#{troca}%"]) if troca.present?
products = products.where(["estado_id LIKE ?","%#{estado_id}%"])
return products
end
end
Search controller
def show
#search = Search.find(params[:id])
#estado = Estado.all
#products = #search.search_products.paginate(:page => params[:page], :per_page => 2).order('id DESC')
end
and search show
<%= will_paginate #products%>
I think that is missing something ;S Please guys, give me a north of how to solve this problem ;S
ps: To make the Search i followed the Railscast tutorial.
This might be quite simple question.
Here's my code. Then I want to sort #codes ordered by the column code.user.last_active_at
#here, I retrieve only the records that contains particular keyword.
#search = Code.search do
fulltext params[:search]
with(:community_id, community_search)
paginate :page => 1, :per_page => 1000000
end
#Here, I try pagination. 10 records per each page.
#codes = #search.results
#codes = Kaminari.paginate_array(#codes).page(params[:page]).per(10)
I want to sort them ordered by the column #code.user.last_active_at
How can I do that?
I tried this
#codes = Kaminari.paginate_array(#codes.joins(:user).order('user.last_active_at DESC')).page(params[:page]).per(10)
But it returned this error
NoMethodError (undefined method `joins' for #
<Sunspot::Search::PaginatedCollection:0x0000001ca851a8>):
app/controllers/codes_controller.rb:95:in `index'
How can I sort while I'm using Kaminari pagination?
Kaminari not affect on sorting. first you have to sort and then get page(x).per(x)
so for example in your controller
#codes = #search.results
#codes = #codes.joins(:user).order('user.last_active_at DESC').page(params[:page]).per(10)
edit:
after check sunspot documentation I think you can make something like
#search = Code.search(include: :user) do
fulltext params[:search]
with(:community_id, community_search)
order_by('user.last_active_at DESC')
#paginate :page => params[:page], :per_page => 10 you can add this and not use Kaminari
end
you can try joins except include
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