searchkick to show only with condition - ruby-on-rails

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!

Related

searchkick requests together with active record requests rails

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.

Ordering Searchkick's search result by act_as_votable vote score

I'm using act as votable for voting on my rails app and searchkick for search. But i want searchkick's search results to be order by vote score. I really need guys, please anybody? Here is what I have at the moment. And it's not working
def search
if params[:search].present?
#peegins = Peegin.search(params[:search]).order(:cached_votes_score => :desc)
else
redirect_to peegins_path
end
I finally figured it out. The problem was the bracket around the search params.
Here's the fix.
#peegins = Peegin.search params[:search], order: { cached_votes_score: :desc}

ElasticSearch rails : order results by object's associated data

I have implemented Searchkick in a rails app and it is working fine. I want to order my results based on the number of associated objects.
For Eg. If I am searching for users by their name, then I want to order the results by the number of followers. (User having most followers should come first)
#users = User.search "2% #{query}", include: [:followers], fields: [:name]
Thanks for your help in advance
I think you need to add counter cache (http://guides.rubyonrails.org/association_basics.html 4.1.2.3) and use followers_count column on User model to order it.
User.search "2% #{query}", include: [:followers],
fields: [:name], order: followers_count

Rails- Merge a find with 2 models

I want to build a rails request with 2 models.
I think it's quite simple, but I don't want to do a loop myself.
I'm in my country model:
def self.find_for_user(user_id)
wines = Wine.where("user_id = ?", user_id).group(:country_id)
where("countries.id IN ?", wines.map())
end
I want to get all countries depending the first request (the wines grouped by countries, I just need the countries)
I think I can do this in a single line where I put map() or another instruction. I just need to get all country_id fields for wines.
Thanks.
Assuming that you've got an association set up between wines and country (ie. has_many :wines in country.rb), I think this is what you're looking for:
def self.find_for_user(user_id)
joins(:wines).where('wines.user_id = ?', user_id).uniq
end
If all you want is all countries that have wine for a specific user, you can do that in SQL:
where("countries.id in (select country_id from wines where wines.user_id = ?)", user_id)

Join tables when searching using Sunspot/Solr in Rails 3

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

Resources