Rails Will Paginate problem - ruby-on-rails

I´m paginating with the will_paginate plugin.
I have a popup that adds #products when the user clicks the button Add via an Ajax request.
Note: The #products is paginated
In Ajax Request I update the product-list :
ids = params[:accessories][:id].join(" and id <> ")
#products = Product.find_by_sql("select * from products where id <> #{ids}")
However if I use: #products = Product.find_by_sql("select * from products where id <> #{ids}").paginate
it returns identical array.
So how I could paginate the array again?

Why are you using find_by_sql method?
May be you can write something like this:
ids = params[:accessories][:id].join(",")
#products = Product.paginate(:page => params[:page], :conditions => ["not (id in (?))", ids])
also conditions you can move to product model and replace it with scope(Rails 3) or named_scope(Rails 2).

Already tried:
#products = Product.paginate :conditions => conditions

Related

Sorting a result defined by remote parameters

i have this index action:
def index
limit = params[:limit]
page = params[:page]
sort = params[:sort].split(',')
#term = nil
if params[:search]
#term = params[:search]
#lessons = policy_scope(Lesson).search(#term)
.order("#{sort[0]} #{sort[1].upcase}")
.paginate(page: page, per_page: limit)
else
#lessons = policy_scope(Lesson).order("#{sort[0]} #{sort[1].upcase}")
.paginate(page: page, per_page: limit)
end
end
which is fed by a vuejs frontend with a vuetify datatable and its purpose is to send out an array of lesson objects, filtered and sorted by the frontend.
this works pretty good with default rails..
however, with the mobility gem involved there is no fieldname "title" for example or "title_en", so the order stops working. in mobility you have some "fake column names" and it automagically handles it to search for the value in a key-value table ( https://github.com/shioyama/mobility#getting-started )
so i sat me down and fired up the console and figured out that:
.order(title: :desc) - works
.order(title_en: :asc) - works
everything with strings involved, like .order('title DESC') or .order('title_en ASC') does not work and results in an error like
ActionView::Template::Error (PG::UndefinedColumn: ERROR: column
"title_en" does not exist LINE 1: SELECT "lessons".* FROM "lessons"
ORDER BY title_en ASC LIMI...
is there a way i could get this working? maybe there is something to generate title: out of 'title'? Or some other magic?
thanks!
You can call the order method like this instead:
.order(sort[0] => sort[1])
Passing "a" => "b" to a method is the same as passing "a": "b".

Rails - Combining will_paginate and ransack

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

find all result when select category all in rails

actually i have a problem as i want to search all the result when i select All from category.As now i have 4 options in my category. I have a query to find the result against three categories like tha,
This shows the result against 3 categories like againt home,apartment and plaza,But when i select all then nothing happen.What i should do that when i select all then all the properties will show.Thanks
Try something like that :
#properties = Property.includes(:rooms)
#properties = (params["search"] == 'ALL') ? #properties.all : #properties.where(params["search"])
#properties = #properties.paginate(:page => params[:page], :per_page => 8)
In my code, I suppose the value for 'All category' would be 'ALL'

Add a field to a ".where" statement after I use it to retrieve a relationship in Rails

I am currently trying to get all the listings from a user's friends (array) AND the user themselves.
I have tried:
ids = current_user.friends << current_user
#listings = Listing.where(:user_id => ids)
but when I use this heroku is crashing. Fundamentally what i want to get is
#listings = Listing.where(:user_id => current_user.friends OR :user_id => current_user)
How can we get this to happen?
You can do it in two ways. By array of ids or array of object it self. Not active record object. Because it's not appending object properly if you use active record object.
Considering friends are also from user table.
ids = current_user.friends.to_a << current_user
#listings = Listing.where(:user_id => ids)
or
ids = current_user.friends.pluck(:id).to_a << current_user.id
#listings = Listing.where(:user_id => ids)
If friends id fetch from other table called friends with one to many association then.
ids = current_user.friends.pluck(:user_id).to_a << current_user.id
#listings = Listing.where(:user_id => ids)
You need to pass array of IDs, try this
#listings = Listing.where("user_id IN (?)", current_user.friends.pluck(:user_id))

How can I order when using pagination?

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

Resources