Rails query where associated (joined) 'rich_text' is not nil/empty - ruby-on-rails

I have the following right now, which works successfully to produce a list of users, with their rich_text_tlk_with_me 'rich_text' attachments.
User.joins(:rich_text_tlk_with_me).paginate(page: params[:page], per_page: 5).order(created_at: :desc)
However, right now this query is also displaying users who have empty rich_text attachments.
I would like to run a query which only returns the users that have a non empty rich_text attachment.
I have tried:
User.joins(:rich_text_tlk_with_me).where.not(rich_text_tlk_with_me: [nil, ""]).paginate(page: params[:page], per_page: 5).order(created_at: :desc)
But this did not return anything.
I have also searched for documentation, but not found anything relevant.

Assuming user has_many :rich_text_tlk_with_mes and you are using Rails version > 5.
You can get all users who have a associated rich_text_tlk_with_me by the following query.
User.left_outer_joins(:rich_text_tlk_with_mes).where.not(rich_text_tlk_with_mes: {id: nil}).paginate(page: params[:page], per_page: 5).order(created_at: :desc)

Related

Pagination with Rails 5 - Having two models on "newsfeed" and paginating the total?

I am building a web app that has a "newsfeed" with two models (called Event and Suggestion). Is it possible to paginate the newsfeed such that the pagination applies to the total and not either individually? Here is what I have so far in my controller. I tried simply adding the two, though that doesn't work because the result is an array, not an ActiveRecord Association. Thanks in advance!
def index
#ideas = Suggestion.paginate(:page => params[:page], per_page: 25).order('created_at DESC')
#ideas += Event.paginate(:page => params[:page], per_page: 25).order('created_at DESC')
render :index
end
Assuming you use 'will_paginate', try this. Put this in your initializers:
require 'will_paginate/array'
then you can do things like this:
#objects = (Suggestion.all + Event.all).sort_by &:created_at
#objects.paginate(:page =>params[:page], per_page: 25)
The problem here is performance. This way you will get all records from the database every time, and then perform pagination

Paginate from record

I use the gem will-paginate.
Lets suppose i have a model records that is sort by created_at and the client has the records until a specific record with the id 77. Now would it be possible to define for example:
Records.paginate(:page => params[:record_id], :per_page => 30)
So that the pagination doesnt`t start at a specific page but at a record
Thanks!
The unique alternative I can imagine is to add a where condition and sorting the results by id
Records.where("id >= ?", params[:record_id]).order(id: :asc).paginate(:page => params[:page], :per_page => 30)
This way you ensure that the record_id received is the first element in the pagination and all the results are after that one

order by association's count of another model, limit to 1

I want to display a gallery of magazines that shows the newest issue for each magazine title, but ordered by the number of subscriptions on that magazine. I came up with this query
class IssuesController < ApplicationController
def index
#issues = Issue
.includes(:magazine)
.order('magazines.subscriptions_count DESC')
.order(:release)
.paginate(page: #page, per_page: 25)
end
end
It correctly orders them by the suscriptions_count, however there are many entries for each magazine, each issue is displayed in order. I just want the first magazine for each one. I've tried adding uniq to the query, group(:magazine_id), limit(1), and more but I can't get it to only display one issue per magazine title.
I found this answer PostgreSQL -must appear in the GROUP BY clause or be used in an aggregate function
which lead me to select("DISTINCT_ON('magazine_id') *"). Works perfectly, only showing a single entry for each magazine_id
def index
#issues = Issue
.includes(:magazine)
.select("DISTINCT ON (magazine_id, magazines.subscriptions_count) *")
.order('comics.subscriptions_count DESC')
.paginate(page: #page, per_page: 25)
end

Rails: How to display users having specific criteria in search?

When User ABC clicks on the "Search" link, I want ABC to see only those members who live in the same city as ABC. Right now I have the following code in the index action of the Users controller, which displays all the members registered on the website. I am also using will_paginate gem for pagination.
def index
#users = User.paginate :page => params[:page], :per_page => 10
end
In the view, I am iterating through the #users array to display all users.
However, I want ABC to only see members from his/her city. Once ABC can only see members from his/her city, I am going to implement filters to further narrow the results. But that is a later step. Also, ABC should not be able to see profiles of users from other city by simply typing their username in the address bar. How do I do this?
You could use ActiveRecord scopes (Ruby on Rails Guides / ActiveRecord Query Interface)
Then in your User model you will have something like this:
scope :living_in_the_same_city_with, lambda { |user| /* your where condition where(:city_id => user.city_id) */ }
And inside controller:
#users = User.living_in_the_same_city_with(current_user).paginate :page => params[:page], :per_page => 10

rails tire elasticsearch weird error

I have indexed a Car model with one car record mercedes benz in the database. If I search for the word benz I get an error:
ActiveRecord::RecordNotFound in CarsController#index
Couldn't find all Cars with IDs (1, 3) (found 1 results, but was looking for 2)
If I search for hello I get:
Couldn't find Car with id=2
Other random search terms work returning accurate results.
So it's basically random errors generated by random search terms. What could be the cause of this?
Controller:
def index
if params[:query].present?
#cars = Car.search(params)
else
#cars = Car.paginate(:page => params[:page], :per_page => 10)
end
end
Model:
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 10) do |s|
s.query { string params[:query]} if params[:query].present?
end
end
This happens because, you are using the load => true option to load the search results from database. The activerecord seems to be missing in DB, but the elasticsearch index contains the same document.
Reindex is not always the solution IMHO.
The best solution is to delete the document when it is deleted in db. You can use the after_destroy callback for this.
Tire remove api is used to remove a single document from index.
Tire.index('my-index-name').remove('my-document-type', 'my-document-id')
Reference: https://github.com/karmi/tire/issues/43

Resources