I'm building a rails app using rails geocoder gem to search the nearby locations given a zip and kaminari to paginate the search results. All went well until I tried using per(2) method to display 2 results per page, then some of the query results got lost. The per method worked fine when used without geocoder filter. The following is my code in the controller:
if params[:search].present?
#users=User.near(params[:search], 5).order(:id).page(params[:page]).per(2)
else
#users = User.order(:id).page(params[:page]).per(2)
end
Would really appreciate it if someone could shed some light into this issue. Thanks!
I believe this problem was fixed in kaminari 0.13.0 gem. Could you try the newest gem?
Related
When using mongoid with 'will_paginate' gem, it seems like method called 'total_entries' doesnt work which can affect perfomance
So I was using Mongoid with 'will_paginate' gem and I had this problem with 'total_entries' option, and to be more specific, I dont think it was working at all, which is not very good for perfomance in many situations...
After a while I found this gem but it didnt look like 'total_entries' method was working there so here's what I did:
1) Opened mongoid_paginator in will_paginate_mongoid location
2) Made a little fix, so it looked like this:
WillPaginate::Collection.create(options[:page], options[:per_page]) do |pager|
items_count = options[:total_entries] || self.count
fill_pager_with self.skip(options[:offset]).limit(options[:per_page]), items_count, pager
which I took from here and hallelujah, it works again, no more counts on every load.
Since I am a newbie I could make something foolish and I hope I will be corrected if so and hope it'll be helpful for somebody :)
I'm trying to do pagination using the will_paginate gem:
#books = Book.joins(:ads).last(20).page(params[:page]).per_page(10)
But I'm getting this error: undefined method `page' for #<\Array:0x007fc3ef37d308> and I can't seem to figure out what's wrong. Pagination works like a charm in other actions.
Thanks! :)
Don't use last as that will trigger the query execution. Use reverse_order and limit instead.
Book.joins(:ads).reverse_order.limit(20).page(params[:page]).per_page(10)
If you still want to paginate array:
require 'will_paginate/array'
and then use
Array#paginate
https://github.com/mislav/will_paginate/wiki/Backwards-incompatibility (the very bottom of the page)
sources:
https://github.com/mislav/will_paginate/blob/2-3-stable/lib/will_paginate/array.rb
https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb
I am using Kaminari for pagination and trying to use meta_search for column ordering. I would like my code to look like this:
#search = Organization.search(params[:search])
#organizations = #search.page(params[:page])
When I write it this way, I am getting the error,
undefined method `page' for #<MetaSearch::Builder:0x7fadb8958630>
The solution I have found is this:
#search = Organization.search(params[:search])
#organizations = Kaminari.paginate_array(#search.all).page(params[:page]
It works but feels clunky. All the examples I have found suggest the first example should work out of the box. Is there any way to turn the MetaSearch result into an ActiveRecord compatible object?
Try a newer version of meta_search +1.0 it provides integration with Kaminari.
I'm upgrading my project to rails 3.0.1 , i have used paginate_all_by_something in the controller in rails 2.1.1 and rails 2.3.8 working fine but in rails 3.0.1 it displaying the undefined method `paginate_all_by_receiver_deleted' for #
error like this
If anybody faced like this problem kindly help me.
It might be because of backwards incompatibility of new version of will_paginate (~>3.0).
Read here for further details.
You can use paginate on collections instead like this:
User.find_all_by_receiver( receiver_id ).paginate
So it's up to you how you construct the collections.
I am using the geokit gem and plugin with rails 3. It seems there is a known issue with them, which can be seen here http://github.com/andre/geokit-rails/issues#issue/15
Now, I tried to follow the solution provided at the bottom. I pasted that function definition, at the end of the file, just above acts_as_mapable, and just after the first time it was called, but nothing happened each time.
Any idea what else can be done?
Thanks
I ran into similar problems upgrading my app to rails 3. I'm still using Geokit for geocoding but Active Record scopes for distance based database queries. It's pretty convenient, and you still get all of the Active Record 3 goodness. Here's an example from my User model:
scope :near, lambda{ |*args|
origin = *args.first[:origin]
if (origin).is_a?(Array)
origin_lat, origin_lng = origin
else
origin_lat, origin_lng = origin.lat, origin.lng
end
origin_lat, origin_lng = deg2rad(origin_lat), deg2rad(origin_lng)
within = *args.first[:within]
{
:conditions => %(
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(users.lat))*COS(RADIANS(users.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(users.lat))*SIN(RADIANS(users.lng))+
SIN(#{origin_lat})*SIN(RADIANS(users.lat)))*3963) <= #{within}
),
:select => %( users.*,
(ACOS(COS(#{origin_lat})*COS(#{origin_lng})*COS(RADIANS(users.lat))*COS(RADIANS(users.lng))+
COS(#{origin_lat})*SIN(#{origin_lng})*COS(RADIANS(users.lat))*SIN(RADIANS(users.lng))+
SIN(#{origin_lat})*SIN(RADIANS(users.lat)))*3963) AS distance
)
}
}
Here's a blog post with a little more detail on the subject: http://stcorbett.com/code/distance-queries-with-rails-3-without-geokit/
jlecour's port to rails 3 should solve any issues you were having last year.
Make sure you're using mysql or postgres if you're doing distance calculations.
After trouble installing the geokit-rails3 gem on Rails 3.1 I moved to the geocoder gem. It has distance calculation as well (be sure to not forget the s in #your_model.nearby*s*(5)). There is also a Railscast.
Here is port of geokit to rails 3, incomplete through:
https://github.com/jlecour/geokit-rails3
For those still having trouble with geokit, i moved on to using mongodb... which has inbuilt distance search n all...
Hey Amit, Not sure if you sorted this out yet but I'll tell you what I did just in case.
I forked andre's geokit-rails source and then cloned it locally and added the code from this gist at line 34 of lib/geokit-rails/acts-as-mappable.rb, just after the line that reads module ClassMethods # :nodoc:.
Then I commited those changes back to my forked repo on github and used my fork to install the source as a plugin to my rails 3 app. That seemed to work straight away, but make sure you have the acts_as_mappable line added to whatever model you are wanting to do distance calculations on and make sure you have two float columns on that db named :lat and :lng.