Using searchkick gem how would I translate this to the .search way? Or is it not doable?
#projects = Project.joins(:proj_status)
Just wasn't sure how to do joins.
eager loading example from gem reference
Product.search "milk", includes: [:brand, :stores]
for your code above
#projects = Product.search "milk", includes: [:proj_status]
If you want to search for projects with status then you might need to do in the following way
in your project model where searchkick included try this
this is for indexing data
I assume Project has name and id and its associated model proj_status has a title.You may change the values depend on your model attributes
def search_data
id: id,
name: name,
status: proj_status.title
end
After Project.reindex
Then you can query Project model with status like,
#projects = Product.search "you query if any", includes: [:proj_status], where: {status: 'open'}
Related
I have a trouble. For example I have a model Article, and my search_data method below
def search_data
title: title,
body: body
end
And I need to receive some records from my controller according to some attributes, for example:
def index
#articles = Article.where(user_id: user_id).search(query)
end
But this approach received all data whos according to query in search method ignoring where method, while I need to search by search method among data received by where method. How to resolve this?
Update
Desirely to use where before search method, not the inside it
It appears SearchKick doesn't allow you to chain a search onto an ActiveRecord Relation like that. It uses ElasticSearch to search instead of your database so if you did that you would be querying two databases for the information. However, it does provide a where option for the search method:
Article.search(query, where: {user_id: user_id})
If you absolutely have to make the ActiveRecord query first, you could get the IDs from the first query and provide them to the second.
ids = Article.where(user_id: user_id)
Article.search(query, where: {id: ids})
The key here tis to make sure that every attribute in your where query also exist in your search_data method. Example:
Message.search search_term, where: {or: [[{user_id: current_user.id}, {recipient_id: current_user.id}]]}
I am looking to find messages with the search_term where the current user is either sender or recipient.
Therefore in my search_data method (in model.rb) I should have:
def search_data
{ user_id: user_id,
recipient_id: recipient_id,
title: title,
description: description,
}
And this works well. this is using Searchkick gem.
I am currently working on a Customer Management System. I am using searchkick to search customers from my customers table. What i am trying to do is to limit the search for each user to only be able to search within their own customer list. Each customer has their own user_id column and i would like searchkick to only search within the users own customers with his/her user_id.
This is my customer_controller.html.erb
def index
#customers = Customer.search(params.fetch(:q, "*"),
order: {name: :asc})
end
can anyone help me? thanks so much!
You can try finding the customer first before doing the search, so something like:
def index
#customer = Customer.find_by(params[:id])
#customers = Customer.search(params.fetch(:q, "*"),
order: {name: :asc})
end
I have figured it out by adding the code:
where: {user_id: current_user.id})
into the controller
Thanks to all that tried to help!
I am kind of new at rails, currently using version 3.23 and I am trying to enable 'sort' on two columns in my table. I managed to create the links on these column headers and actually got one column working/sorting!But couldnt achieve the same result when I modified my code in the movie controller rb, my code can only work for one column! is:
def index
#movies = Movie.all.sort_by { |movie| movie.title }
end
Works perfectly, but when I combine another parameter i.e. release date I get an error!
def index
#movies = Movie.all.sort_by { |movie| movie.title } then { |release date| release.date}
end
Can someone please help me resolve this issue? I have researched it on google but I've gotten nothing conclusive!. Any help will be most appreciated.
Assuming that you have to sort on title and release_date fields in movies table.
You can perform the sorting at database level itself as below:
In Rails 4.x:
Below will sort all the movie records with title and release_date in ascending order(as default).
def index
#movies = Movie.order(:title, :release_date)
end
If you want to change the order, you can specify as asc or desc as:
def index
#movies = Movie.order(title: :asc, release_date: :desc)
end
In Rails 3.x:
def index
#movies = Movie.all(:order => "title ASC, release_date ASC")
end
If you want to change the order, you can specify as DESC in the above case.
You can order your listing by this
def index
#movies = Movie.order(title: :asc, release_data: :desc)
end
How can I get collection of attributes for search result?
I.E.
I search through Product model which have many Category:
class Product < ActiveRecord::Base
has_many :categories
Here is my index:
ThinkingSphinx::Index.define :product, with: :active_record do
###
has category(:id), as: :direct_category_id
So, I search through query
products = Product.search params[:query]
categories = Category.find(products.map(&:category_id)) # slow & bad & ugly
But this method is so slow and bad. Is there any way to get all atttributes from search results instead collect?
search = Product.search params[:query]
search.context[:panes] << ThinkingSphinx::Panes::AttributesPane
category_ids = search.collect { |product|
product.sphinx_attributes['direct_category_id']
}
categories = Category.find category_ids
However, keep in mind that if you run this in the console the first line evaluates the search request because IRB renders the result. This means the pane can't be added... so you'll want to add ; '' or similar at the end of the first line (again: only necessary in a Rails console):
search = Product.search params[:query]; ''
okay. I solved by myself. The solution use facets
First of all, we need to add direct_category_id:
has category(:id), as: :direct_category_id, facet: true
After that, we need just to use
category_ids = products.facets[:direct_category_id].keys
categories = Category.where(id: category_ids)
I have an ActiveRecord model Products with associated Suppliers (via belongs_to/has_many association). I am using Sunspot for full-text searching. I make a search with that code:
#search = Products.search do
fulltext params[:search]
end
#products = #search.results
But I'd like to include suppliers too, so every time I call, for example,
#products.first.supplier
it wouldn't make a new request to the database. I tried to use
#search = Products.search(include: :supplier) do
but it didn't help. Is there any possible way to do that in Sunspot?
You can try this
#search = Sunspot.search[Products, Supplier] do
.....
end