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
Related
I want to return all of the profiles found except for one, the one of the current user. I was thinking of doing this from the model.
self.order('random()')
.where(:blahblah => blah)
So everything is working up until here
I added this part, and it doesn't work
.not(:email => "blah#blah.com")
And I am not sure why. The documentation shows queries that are like where.why(blah) and nothing like what I am doing. I am not sure where else to look. Thanks.
Getin NoMethodError
undefined method `not' for #
Depending on which rails version you are on,
if rails 3
self.order('random()').where('email != ?', "blah#blah.com")
if rails 4
self.order('random()').where.not(email: "blah#blah.com")
I included thinking_sphinx gem in my project.
Article.search "Bla-bla-bla" works fine.
But when I include ransack gem in my Gemfile I get the error:
NoMethodError: undefined method `with_indifferent_access' for "Bla-bla-bla":String from /Users/ashvalev/.rvm/gems/ruby-1.9.2-p290/gems/ransack-0.7.2/lib/ransack/search.rb:21:in `initialize'
It is because ransack gem also uses name "search" for its searching method.
What can I do to make these gems work together?
The solution is to call ThinkingSphinx.search instead of the specific model's #search method.
Instead of
Article.search "Bla-bla-blah"
Use the system-wide search call, but constrain to the class...
ThinkingSphinx.search "Bla-Bla-Blah", classes: [Article]
You'll want to list ransack before thinking-sphinx in your Gemfile - the load order matters.
Though I would certainly recommend just using one of the two when it comes to searches.
Why does Post.page(1).total_pages result in:
Post.page(1).total_pages
undefined local variable or method `total_pages' for #<ActiveRecord::Relation:0x00000006a95230>
but
Post.scoped.page(1).total_pages
works fine. Curiously,
Post.paginate(:page => 1).total_pages
works fine. I looked at the code on Github ( https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/active_record.rb ) and I can see why paginate works (because it calls limit first... which returns an active record relation, much like scoped does). I have a feeling it has something to do with this code
rel = scoped.extending(RelationMethods)
I guess I don't understand the difference between these the active record relation that limit returns versus scoped.extending(RelationMethods). Any ideas?
This only happens when using the rails_admin gem. It works fine in a fresh Rails 3.1.1 app with will_paginate 3.0.2.
Rails admin is probably doing something to the page method, though I'm not sure what exactly.
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?
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.