Issue with Kaminari while using meta_search for column ordering - kaminari

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.

Related

Issue constructing a query in Rails using Arel and array attributes

I'm stuck with the query that checks whether two arrays have one or more elements in common.
Since the query is the part of a much bigger query, I need to use Arel to accomplish this. But the && operator, known as the overlap operator, is not implemented in Arel gem.
There's a postgres_ext gem that implements the above mentioned operator and provides a .overlap method so that one could construct the query similar to the one I did: DiscountCondition.arel_table[:target_plan_period_ids].overlap(target_period_ids). This produces an SQL where clause that works fine for me: "\"discount_conditions\".\"target_plan_period_ids\" && '{2}'".
But. The thing is some tests in our application failed with the following error: NoMethodError: undefined method 'array' for #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x007f23c4995ba8> (turns out the gem is incompatible with some adapters).
A simple ActiveRecord query that works is DiscountCondition.where('target_plan_period_ids && ARRAY[?]', target_period_ids) that produces the following SQL query "SELECT \"discount_conditions\".\"discount_id\" FROM \"discount_conditions\" WHERE (target_plan_period_ids && ARRAY[2])".
So, I wanted to know if anyone faced that issue and succeeded to workaround this.
Just in case someone ever faces the same issue, I ended up monkey patching the aforementioned functionality of a postgres_ext gem into the project.

paginate last 20 models, ruby on rails (will_paginate gem)

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

Thinking_sphinx Search method doesn't work when I include ransack gem in my Gemfile

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.

problem with pagination using kaminari

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?

Where should I start looking to create a plugin that extends Rails 3 finds?

Prior to Rails 3, creating a plugin to extend find was relatively easy: basically override the find method itself in ActiveRecord::Base, calling super if needed.
Now that Arel is being used in Rails 3, (specifically I'm using Rails 3.1), how would I go about doing something similar? The problem is that many of the old find methods are deprecated in favor of scopes like where, order, limit, etc. At what point(s) in the Rails source should I try to override the default behavior?
I'm sure it's going to be a bit more convoluted than this, but the closest thing I can find that seems like it might be appropriate is the construct_finder_arel method in ActiveRecord::Base.
After digging through Rails source, regardless of Arel being used, find_by_sql method is called on whatever model is performing the find. This can be extended via alias_method_chain as follows:
find_by_sql_with_customization(*args)
results = find_by_sql_without_customization
# do something with results here
results
end
alias_method_chain :find_by_sql, :customization

Resources