Meilisearch Filter Rails Array - ruby-on-rails

I am looking for a better way of filtering an array of values through Maeiliseach using an array. I have users that can have multiple accounts and want to search all of a users accounts inventory. I have indexed the Inventory items.
This what a current do
Inventory.search("",{filter: ['account_id = 4 OR account_id = 5']})
I can use
accounts = user.accounts.pluck(:id)
to get all the id's. Once I have them I can turn it into a string for answer like this 'account_id = 4 OR account_id = 5'. But seems like that might not be the best way and I am missing something. Any help much appreciated. The documentation didn't seem to have the answer I was looking for but maybe I missed it.

I had the wrong version of pagy that is why I was getting the errors.

Related

Finding the number of models with a certain attribute

Hi I have a model in my database User, a user can have zero, one or many Cars. I want to find all the Users in my database with one or more Cars. At the moment I have logic that says:
#car_user_count
for user in Users.all do
if user.cars.count > 0
#car_user_count = #car_user_count + 1
end
This seems like an overly complicated way of doing this. I assume there must be a way of doing something like:
User.where(Cars.count > 0).all.count
but I just can't seem to get it to work, any help with this would be greatly appreciated.
I want to find all the Users in my database with one or more Cars.
User.includes(:cars).where.not(cars: { id: nil })
For your example try joins with count:
User.joins(:cars).distinct.count

Display Sorted Column on Static Page in Rails

I am creating a website that allows users to evaluate their coworkers. My boss would like the averages to be displayed from best to worst on a static page that she can print and hang up in our store, so the employees can see their results compared to other employees. I have been searching for awhile now on how to easily sort a column. I found a Railscast on sorting columns, but it seems a lot more detailed than I truly need. I found the order API, but I don't think I'm implementing it the way I need to. I am hoping that maybe there is a one-liner that can help me solve this problem, such as:
#user = User.all.order(average: :asc)
Where I can load a static page that prints the user's name and their score. Thank you in advance!
Have you tried that code of yours? It should do exactly what you're asking except from lowest score to highest score.
You could simplify it a little and sort from highest to lowest by doing:
#users = User.order(average: :desc)
Like MarsAtomic said, this assumes that you actually have a column in your users table called average. If not we need more information on how your database is set up.

How to remove some items from a relation?

I am loading data from two models, and once the data are loaded in the variables, then I need to remove those items from the first relation, that are not in the second one.
A sample:
users = User.all
articles = Articles.order('created_at DESC').limit(100)
I have these two variables filled with relational data. Now I would need to remove from articles all items, where user_id value is not included in the users object. So in the articles would stay only items with user_id, that is in the variable users.
I tried it with a loop, but it was very slow. How do I do it effectively?
EDIT:
I know there's a way to avoid doing this by building a better query, but in my case, I cannot do that (although I agree that in the example above it's possible to do that). That thing is that I have in 2 variables loaded data from database and I would need to process them with Ruby. Is there a command for doing that?
Thank you
Assuming you have a belongs_to relation on the Article model:
articles.where.not(users: users)
This would give you at most 100, but probably less. If you want to return 100 with the condition (I haven't tested, but the idea is the same, put the conditions for users in the where statement):
Articles.includes(:users).where.not(users: true).order('created_at DESC').limit(100)
The best way to do this would probably be with a SQL join. Would this work?
Articles.joins(:user).order('created_at DESC').limit(100)

sunspot/solr multiple indices

I have sunspot/solr set up to search products on my site. We need the ability to search users and another model (too much to explain what this is) in out app. Basically there is form for searching product via solr and this works well. There would be another form for searching users and the other form to search the other model.
I assume it is recommended to have a separate index for products, users, and the other model? It's seems best to keep the index from getting too bloated? Am I on the right track here?
All the models are indexed in the same index. And sunspot will also index the classnames into the index.

Returning Search Results in Rails

I am having a problem implementing a special kind of search for my Rails application. I am working on an achievement system where you can search for a set of users in a search form (e.g., the query being "Ross, Adam, Jake") and it returns all of the common achievements that the users have unlocked (e.g., if users Ross, Adam, and Jake all had an achievement named "You are winner!"). I have three tables, one for achievements, one for users, and a join table. We have tested the associations and such, so we know that works.
My first idea was to put the search terms in an array and get the search results for each item in the array and place them into respective "search result arrays". Then, I was thinking to go through each item in search result array 1 to see if it appears in both of the other result arrays. The objects that appear in all three of the search result arrays would be returned and displayed on a page.
Is there an easy way to implement this without writing a bunch of my own code? Are there some functions I should know about? Any help will be appreciated!
Well, both Ransack and it's predecessor (MetaSearch) are useful gems for creating complex search forms.
In general I think you want to do something like select distinct achievement ids for user ids in an array. Off the top of my head I'm not quite sure how you should write it... others may know.
Look at the documentation on MetaSearch (more established) and see if you see a pattern that fits, if not check Ransack (more advanced).
You can use some autocomplete plugin for user names and convert the names to ids on the fly, that way you won't have to deal with converting user names to ids in backend later.
For common achievements, if a user can have a achievement only once, aggregating the results in join table and counting the results with achievement ids would be the way to go.
You can provide more details for a more detailed answer. :)
You can use Sunspot which is allows easy solr integration with Ruby and Rails

Resources