Is there a way in which we can paginate using with_exclusive_scope in Rails 2.3?
#albums = Album.paginate(:page => 1, :per_page => 12, :conditions => cond)
works fine.
Album.with_exclusive_scope {find(:all, :conditions => cond)}
works fine.
Can we combine with_exclusive_scope and paginate?
#albums = Album.find_allx(opts).paginate(:page => 1, :per_page => 12)
with_exclusive_scope is a protected method, so it has to be called from a class message. In this case the class method is find_allx()
Related
I am able to install this app on my windows 7 laptop https://github.com/cheezy/puppies
But now when I am trying to access it at localhost:3000 it is giving me error:
unsupported parameters: :order
I went to the file in this app and found this code:
app/controllers/agency_controller.rb
class AgencyController < ApplicationController
skip_before_filter :authorize
def index
#puppies = Puppy.paginate :page => params[:page], :order => 'name', :per_page => 4
end
end
While looking for a fix on this error I found a fix here https://github.com/mislav/will_paginate/issues/500
Where a comment by "mislav" says that "Active Record doesn't support :xyz formatting and it need to be written in User.where(conditions).order('title').per_page(per_page).page(page) format.
So, if it is a fix how to write #puppies = Puppy.paginate :page => params[:page], :order => 'name', :per_page => 4 in suggested format?
But if its not actual fix how to fix it?
I think that's because you are passing :order into the paginate method.
The correct query should be:
#puppies = Puppy.order(:name).paginate(page: params[:page], per_page: 4)
I am displaying a list of gyms, and users can view gyms by province. I would like to add search capability using the Ransack gem, but I am not sure how to go about it with my current set up. Looking for some advice.
Gyms controller
def index
if params[:province].blank?
#gyms=Gym.all.order("created_at desc").paginate(:page => params[:page], :per_page => 18)
else
#province_id = Province.find_by(name: params[:province]).id
#gyms = Gym.where(province_id: #province_id).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)
end
end
thank you in advance.
Figured it out. Just replaced my existing controller code with this:
#q = Gym.ransack(params[:q])
#gyms = #q.result.includes(:province).page(params[:page]).order("created_at DESC").paginate(:page => params[:page], :per_page => 18)
I am replacing will_paginate with Kaminari and large part of my application uses .paginate method with multiple conditions.
#posts = Post.published.paginate(:order => 'published_at desc, id',
:joins => :comments,
:conditions => conditions,
:group => "posts.id",
:per_page => 10,
:page => params[:page])
I am looking for quickest way and the most efficient way to replace such code with kaminari.
Rather then
#posts = Post.published.paginate.order('published_at desc').....page().per()
The following result sets work well with will_paginate:
Members.all(:limit => 5).paginate(:page => params[:page])
Members.all(:conditions => ["member_no < 6"]).paginate(:page => params[:page])
Members.all.paginate(:page => params[:page])
The following does not:
Members.all(:conditions => ["member_no IN (?)", [1, 2, 3, 4, 5]]).paginate(:page => params[:page])
Why the second query does not work well with paginate? Thanks!
The #paginate is an instance method made available on Array and ActiveRecord::Base. You really should be doing it this way:
Member.paginate(:page => params[:page], :limit => 5)
Member.paginate(:conditions => ["member_no < ?", 6], :page => params[:page])
Member.paginate(:page => params[:page])
Member.paginate(:conditions => {:member_no => (1..5)}, :page => params[:page])
When you call #all then #paginate, what you're doing is asking for all members (all 1,000,000 of them), then discarding 99.999% of them, because you only want the first 10. This is very wasteful, to say the least.
I am upgrading a Rails application from 2.2.2 to 2.3.5. The only remaining error is when I invoke total_entries for creating a jqgrid.
Error:
NoMethodError (undefined method `total_entries' for #<Array:0xbbe9ab0>)
Code snippet:
#route = Route.find(
:all,
:conditions => "id in (#{params[:id]})"
) {
if params[:page].present? then
paginate :page => params[:page], :per_page => params[:rows]
order_by "#{params[:sidx]} #{params[:sord]}"
end
}
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #route }
format.json { render :json => #route }
format.jgrid {
render :json => #route.to_jqgrid_json(
[
:id, :name
],
params[:page],
params[:rows],
#route.total_entries
)
}
end
Any ideas? Thanks!
EDIT
I am able to get it working by removing the block used after find. I also had to move the order_by used by the squirrel plugin as I was getting an undefined method call for it.
I don't like the fact that this is less DRY than the previous code by having to use conditions in more than one location. Is there any better way to do this with Rails 2.3.5, will_paginate, and squirrel?
if params[:page].present? then
#route = Route.paginate :conditions => "id in (#{params[:id]})", :page => params[:page], :per_page => params[:rows], :order => "`#{params[:sidx]}` #{params[:sord]}"
else
#route = Route.find(:all, :conditions => "id in (#{params[:id]})")
end
EDIT 2
Another possibility for this error may be that I was using Ruby 1.8.7 with Rails 2.2.2 and am now using Ruby 1.9.1 with Rails 2.3.5. Were there any major changes between 1.8.7 and 1.9.1 that would prevent the block after the ActiveRecord find to not run?
In Rails 2.3.5, a find(:all, ...) call will return an Array and generally these do not have any custom methods associated with them like you might get with a scope. Passing a block to a find call is also a little irregular and may be part of the problem.
You may be able to fix this by creating a scope that finds what you want:
class Route < ActiveRecord::Base
named_scope :with_ids, lambda { |*ids| {
:conditions => { :id => ids.flatten }
}}
end
Then you can use the scope instead:
#routes = Route.with_ids(params[:id])