activerecord-hackery / ransack Goto Github PK
View Code? Open in Web Editor NEWObject-based searching.
Home Page: https://activerecord-hackery.github.io/ransack/
License: MIT License
Object-based searching.
Home Page: https://activerecord-hackery.github.io/ransack/
License: MIT License
Rails 3.2.0.rc1
Arel 3.0.0.rc1
Ransack 0.5.8
Person.search(params[:q])
fails with error
undefined method `schema_cache' for ActiveRecord::Base:Class
top of stacktrace
activerecord (3.2.0.rc1) lib/active_record/dynamic_matchers.rb:50:in `method_missing'
arel (3.0.0.rc1) lib/arel/visitors/to_sql.rb:11:in `initialize'
arel (3.0.0.rc1) lib/arel/visitors.rb:34:in `new'
arel (3.0.0.rc1) lib/arel/visitors.rb:34:in `block in <module:Visitors>'
arel (3.0.0.rc1) lib/arel/visitors.rb:38:in `yield'
arel (3.0.0.rc1) lib/arel/visitors.rb:38:in `default'
arel (3.0.0.rc1) lib/arel/visitors.rb:38:in `visitor_for'
ransack (0.5.8) lib/ransack/context.rb:37:in `initialize'
ransack (0.5.8) lib/ransack/context.rb:17:in `new'
ransack (0.5.8) lib/ransack/context.rb:17:in `for_class'
ransack (0.5.8) lib/ransack/context.rb:11:in `for'
ransack (0.5.8) lib/ransack/search.rb:18:in `initialize'
The problem is that Arel::Visitors::ToSql#initialize expects connection adapter as argument but it gets ActiveRecord::Base.
This could be also bug of Arel RC so I am sorry if I posted it inappropriately.
Relevant Gemfile:
gem 'rails', :branch => '3-1-stable', :git => 'git://github.com/rails/rails.git'
gem "ransack", :git => "https://github.com/ernie/ransack.git"
Console output:
ruby-1.9.2-p290 :014 > Query.new(:name => "Test").save
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `queries` (`created_at`, `name`, `params`, `updated_at`) VALUES ('2011-07-20 16:35:48', 'Test', '--- {}\n', '2011-07-20 16:35:48')
(14.9ms) COMMIT
=> true
ruby-1.9.2-p290 :015 > @query = Query.find_by_name("Test")
Query Load (10.5ms) SELECT `queries`.* FROM `queries` WHERE `queries`.`name` = 'Test' LIMIT 1
ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a ActiveSupport::HashWithIndifferentAccess, but was a Hash
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/coders/yaml_column.rb:28:in `load'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:1946:in `block in set_serialized_attributes'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:1945:in `each'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:1945:in `set_serialized_attributes'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:1595:in `init_with'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:972:in `instantiate'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:470:in `block in find_by_sql'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:470:in `collect!'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:470:in `find_by_sql'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/relation.rb:111:in `to_a'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/relation/finder_methods.rb:370:in `find_first'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/relation/finder_methods.rb:122:in `first'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/relation/finder_methods.rb:257:in `find_by_attributes'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/activerecord/lib/active_record/base.rb:1070:in `method_missing'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/will_paginate-e1676b5cec9e/lib/will_paginate/finder.rb:170:in `method_missing_with_paginate'
from (irb):15
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/railties/lib/rails/commands/console.rb:45:in `start'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/railties/lib/rails/commands/console.rb:8:in `start'
from /home/brian/.rvm/gems/ruby-1.9.2-p290@lfm/bundler/gems/rails-ff057369478b/railties/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.2-p290 :016 >
Trying to switch from MetaSearch to Ransack and ran into this. Any idea what I could be doing wrong?
# controller
@search = This.search(params[:q])
@these = @search.result.paginate(...).all
<%# view %>
<% search_form_for @search do |f| %>
<%= f.label :name_contains %>
<%= f.text_field :name_contains %>
<% end %>
I would like to be able to, e.g., pass in 'sorts = "user_name asc"' to sort by the user's name field.
Shouldn't be too hard to implement, should it? Checking the logs, the join to the associated table already gets performed, so I'm assuming it'd be trivial to pass in the association's field name in the order(...) call as well?
I am interested in making alias to set of attributes, e.g. instead of writing:
first_name_or_last_name_cont
it would be great to have ability to write simply:
full_name_cont
when somewhere we defines that full_name consists of first_name and last_name
In current sort_link helper in form_helper.rb you use hardcoded param names e.g. params[:q]. This makes impossible to use custom key name (needed for 2 different searches on one page).
Is it possible to do greater_than_or_equal_to or boolean searches yet just like you would using metasearch?
Thanks,
Dan
This is more of a request/am I missing this somewhere
It is easy enough to create inbound links that will pre-populate the simple form, such as:
http://ransack-demo.herokuapp.com/users?utf8=%E2%9C%93&q%5Bfirst_name_or_last_name_cont%5D=&q%5Bemail_cont%5D=&q%5Bposts_title_cont%5D=t&commit=Search
The query params can be easily written by a human. However the query params generated by the Adv. search are much more complicated, the inbound link will filter the results but it will not fill-in the form
http://ransack-demo.herokuapp.com/users/advanced_search?q[first_name_or_last_name_cont]=&q[email_cont]=&q[posts_title_cont]=t&commit=Search
I'm looking over the code for how to modify the core code but I'm just not that good. In the mean time I just reversed the query param's structure and came up with this helper method:
def ransack_query_hash(params)
ret_hash = {}
i = 0
ret_hash['g'] = {}
params.each do |group_type, values|
group = {}
group['m'] = group_type == :all ? 'and' : 'or'
group['c'] = {}
j = 0
values.each do |property,details|
group['c'][j.to_s] = {'a' => {"0" => {'name'=> property}}, 'p' => details[:p].to_s, 'v' => [*details[:v]] }
j += 1
end
ret_hash['g'][i.to_s] = group
i += 1
end
ret_hash
end
This function takes input like:
:all => {
:sub_object_parent_id => {
:p => 'eq_any',
:v => rbs.sector_ids
},
:sub_object_parent_type => {
:p => 'eq',
:v => 'Rbs'
}
}
This will build a param hash understood by the adv search form and hence pre-populate the form.
Is there an easier way to get this done?
The base table is consumers
. We store roles in a consumer_roles
table with a many-to-many association. They're joined through consumer_roles_consumers
, which is basically a join table with created_at/modified_at timestamps.
I'm getting a duplicate join when I try to add the following two conditions:
It produces the following query:
SELECT DISTINCT consumers
.* FROM consumers
LEFT OUTER JOIN consumer_roles_consumers
ON consumers
.id
= consumer_roles_consumers
.consumer_id
LEFT OUTER JOIN consumer_roles
ON consumer_roles
.id
= consumer_roles_consumers
.consumer_role_id
LEFT OUTER JOIN consumer_roles_consumers
consumer_roles_consumers_consumers
ON consumer_roles_consumers_consumers
.consumer_id
= consumers
.id
WHERE ((consumer_roles
.name
= 'Senior Ambassador' AND consumer_roles_consumers_consumers
.created_at
< '2009-01-01 00:00:00')) LIMIT 50 OFFSET 0
Should the second condition not be applied to the first join of consumer_roles_consumers
, removing the second join?
Hi,
I'm trying to use a custom predicate, but I get always the same error.
Ransack.configure do |config|
config.add_predicate 'btw',
:arel_predicate => 'in',
:formatter => proc {|v| Range.new(Date.today, Date.today+1)}
end
Thanks,
Carlos
Failing tests for predicate matches and blank when false:
I've tested ransack with rails 3.2rc1 and it seems there are compatibility issues in the new changes to activerecord. When running the search() method I get the error:
undefined method `schema_cache' for ActiveRecord::Base:Class
Here is a full backtrace of a failing spec. https://gist.github.com/1501784
I might give this a bit of a look over the weekend, but i'm fairly sure I might be in over my head a lil bit :P
I've got an ActiveRecord model with a scope and a ransacker defined like this:
scope :by_country_state, lambda {|state|
ddds = City.where(:country_state => state).map{|city| city.ddd}.uniq!
where(:ddd => ddds)
}
ransacker :by_country_state
If I try to add f.text_field :by_country_state
on the view, when I search I get ArgumentError (No valid predicate for by_country_state)
, if I try to add f.text_field :by_country_state_eq
, I get:
TypeError (Cannot visit ActiveRecord::Associations::JoinDependency::JoinBase):
app/models/telephone.rb:8:in `block in <class:Telephone>'
Line 8 is the second line on my former blockquote.
I'm following this behavior from meta_search search_methods, so I might be taking the wrong approach, but I couldn't figure it out from the specs or the code. Could you help me, please?
Ernie,
I am attempting to migrate from meta_search to ransack and for the most part it has been smooth sailing. I am however getting into a bit of a problem with polymorphic associations.
My models look like:
#order.rb
class Order < ActiveRecord::Base
belongs_to :customer, :polymorphic => true
end
#person.rb
class Person < ActiveRecord::Base
end
#customer.rb
class Customer < Person
has_many :orders, :as => :customer
end
#organization.rb
class Organization < ActiveRecord::Base
end
#institutional_customer.rb
class InstitutionalCustomer < Organization
has_many :orders, :as => :customer
end
(It looks a little convoluted, I know, but Person and Organization serve as STI for a lot of other clases in our internal logic).
In metawhere the following search worked just fine (with _contains instead of _cont of course):
= search_form_for @p do |f|
%p
= f.label :customer_person_type_last_name_or_customer_organization_type_name_cont, 'Name'
= f.text_field :customer_person_type_last_name_or_customer_organization_type_name_cont
%p
= f.submit 'Search'
However ransack has problems with the polymorphic association:
undefined method `customer_person_type_last_name_or_customer_organization_type_name_cont' for #<Ransack::Search:0x00000104b50400>
Any thoughts on this? I tried many ways of going about it, but could not get it working.
Thanks,
Ylan
PS: Thanks a lot for making your gems available: I could not live without them. I just migrated meta_where to squeel and didn't encounter any problem.
Using ruby 1.9.2 p290, rails 3.1.0, ransack 0.5.6 and will_paginate 3.0.0 (also tried latest git), will blow up when you have > 1 page of results. Everything is fine with only 1 page of results. I have no idea whether this is a ransack or will_paginate issue or just the combination of both.
Perhaps I'm doing this wrong, so let me know if this looks ok.
In my controller:
def index
@search = State.paginate(:page => params[:page]).search(params[:q])
@search.sorts = 'name asc' if @search.sorts.empty?
@States = @search.result
end
This line in my view:
= will_paginate @States
will produce::
undefined method -' for nil:NilClass /home/www/cats/cats/shared/bundle/ruby/1.9.1/bundler/gems/will_paginate-e1f4af74351f/lib/will_paginate/view_helpers/link_renderer_base.rb:30:in
windowed_page_numbers'
If I remove the ransack, pagination works fine. Will paginate docs say that it return an active record relation which should be chainable, but I think something that will_paginate is adding is getting clobbered by ransack.
I think the current_page value in windowed_page_numbers (and maybe some other values) are being overwritten thus the error subtracting from nil. Here is the will_paginate code: https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/view_helpers/link_renderer_base.rb
Thanks for any help on this!
Hi
I've been using meta_search a lot - now I try to migrate to ransack. Problem is, I've been using custom search methods a lot, together with scopes, which for me makes perfect sense. Now I can't find a way to use the same feature in Ransack. Do I have to switch back to meta_search or do I miss something?
Firstly.. fantastic gem. It's really helping me out!
I have one problem though.
class Management < ActiveRecord::Base
has_many :instructor_owners, :as => :owner
has_many :instructors, :through => :instructor_owners, :dependent => :destroy
...
In a controller when I try to access
@management.instructors.search(params[:q])
It blows up with:
undefined method `table_aliases' for #<ActiveRecord::Associations::JoinDependency:0x007fb3c78f1510>
This seems to be an issue with :through associations... I having success with any other kind of association.
Full Trace:
ransack (0.5.8) lib/ransack/adapters/active_record/context.rb:133:in block in build_join_dependency' ransack (0.5.8) lib/ransack/adapters/active_record/context.rb:132:in
each'
ransack (0.5.8) lib/ransack/adapters/active_record/context.rb:132:in build_join_dependency' ransack (0.5.8) lib/ransack/adapters/active_record/context.rb:97:in
join_dependency'
ransack (0.5.8) lib/ransack/context.rb:33:in initialize' ransack (0.5.8) lib/ransack/context.rb:17:in
new'
ransack (0.5.8) lib/ransack/context.rb:17:in for_class' ransack (0.5.8) lib/ransack/context.rb:11:in
for'
ransack (0.5.8) lib/ransack/search.rb:18:in initialize' ransack (0.5.8) lib/ransack/adapters/active_record/base.rb:15:in
new'
ransack (0.5.8) lib/ransack/adapters/active_record/base.rb:15:in ransack' activerecord (3.1.0) lib/active_record/relation.rb:455:in
block in method_missing'
activerecord (3.1.0) lib/active_record/relation.rb:180:in block in scoping' activerecord (3.1.0) lib/active_record/base.rb:1190:in
with_scope'
activerecord (3.1.0) lib/active_record/relation.rb:180:in scoping' activerecord (3.1.0) lib/active_record/relation.rb:455:in
method_missing'
activerecord (3.1.0) lib/active_record/associations/collection_proxy.rb:102:in method_missing' app/controllers/admin/instructors_controller.rb:15:in
index'
actionpack (3.1.0) lib/action_controller/metal/implicit_render.rb:4:in send_action' actionpack (3.1.0) lib/abstract_controller/base.rb:167:in
process_action'
actionpack (3.1.0) lib/action_controller/metal/rendering.rb:10:in process_action' actionpack (3.1.0) lib/abstract_controller/callbacks.rb:18:in
block in process_action'
activesupport (3.1.0) lib/active_support/callbacks.rb:470:in _run__2755570935726632965__process_action__4180327824903482234__callbacks' activesupport (3.1.0) lib/active_support/callbacks.rb:386:in
_run_process_action_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in run_callbacks' actionpack (3.1.0) lib/abstract_controller/callbacks.rb:17:in
process_action'
actionpack (3.1.0) lib/action_controller/metal/rescue.rb:17:in process_action' actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:30:in
block in process_action'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in block in instrument' activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in
instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in instrument' actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:29:in
process_action'
actionpack (3.1.0) lib/action_controller/metal/params_wrapper.rb:201:in process_action' activerecord (3.1.0) lib/active_record/railties/controller_runtime.rb:18:in
process_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:121:in process' actionpack (3.1.0) lib/abstract_controller/rendering.rb:45:in
process'
actionpack (3.1.0) lib/action_controller/metal.rb:193:in dispatch' actionpack (3.1.0) lib/action_controller/metal/rack_delegation.rb:14:in
dispatch'
actionpack (3.1.0) lib/action_controller/metal.rb:236:in block in action' actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in
call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in dispatch' actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:29:in
call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in block in call' rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in
block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:110:in optimized_each' rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in
recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in call' actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:531:in
call'
lib/api_defender.rb:48:in call' warden (1.0.5) lib/warden/manager.rb:35:in
block in call'
warden (1.0.5) lib/warden/manager.rb:34:in catch' warden (1.0.5) lib/warden/manager.rb:34:in
call'
actionpack (3.1.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in call' rack (1.3.3) lib/rack/etag.rb:23:in
call'
rack (1.3.3) lib/rack/conditionalget.rb:25:in call' actionpack (3.1.0) lib/action_dispatch/middleware/head.rb:14:in
call'
actionpack (3.1.0) lib/action_dispatch/middleware/params_parser.rb:21:in call' actionpack (3.1.0) lib/action_dispatch/middleware/flash.rb:243:in
call'
rack (1.3.3) lib/rack/session/abstract/id.rb:195:in context' rack (1.3.3) lib/rack/session/abstract/id.rb:190:in
call'
actionpack (3.1.0) lib/action_dispatch/middleware/cookies.rb:326:in call' activerecord (3.1.0) lib/active_record/query_cache.rb:62:in
call'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in call' actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in
block in call'
activesupport (3.1.0) lib/active_support/callbacks.rb:392:in _run_call_callbacks' activesupport (3.1.0) lib/active_support/callbacks.rb:81:in
run_callbacks'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:28:in call' actionpack (3.1.0) lib/action_dispatch/middleware/reloader.rb:68:in
call'
rack (1.3.3) lib/rack/sendfile.rb:101:in call' actionpack (3.1.0) lib/action_dispatch/middleware/remote_ip.rb:48:in
call'
actionpack (3.1.0) lib/action_dispatch/middleware/show_exceptions.rb:47:in call' railties (3.1.0) lib/rails/rack/logger.rb:13:in
call'
rack (1.3.3) lib/rack/methodoverride.rb:24:in call' rack (1.3.3) lib/rack/runtime.rb:17:in
call'
activesupport (3.1.0) lib/active_support/cache/strategy/local_cache.rb:72:in call' rack (1.3.3) lib/rack/lock.rb:15:in
call'
actionpack (3.1.0) lib/action_dispatch/middleware/static.rb:53:in call' railties (3.1.0) lib/rails/engine.rb:455:in
call'
railties (3.1.0) lib/rails/rack/content_length.rb:16:in call' railties (3.1.0) lib/rails/rack/log_tailer.rb:14:in
call'
rack (1.3.3) lib/rack/handler/webrick.rb:59:in service' /Users/morgz/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in
service'
/Users/morgz/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in run' /Users/morgz/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in
block in start_thread'
Hi!
How do I create a ransacker that depends on two arguments?
Example:
I want to search by max_price
based on max_price_amount
and max_price_currency
.
I tried the following
ransacker :max_price, args: [:amount, :currency] do |price_per_hour, currency|
usd_cents = Money.new(price_per_hour.to_i * currency.subunit_to_unit, currency).exchange_to(:usd).cents
Arel::Nodes::InfixOperation.new('<=', parent.table[:price_per_hour_usd_cents], usd_cents)
end
I tried the code below for the view; it generates the view, but on submit gives a different error:
No valid predicate for max_price
= f.fields_for :max_price do |mpf|
= mpf.text_field :amount
= mpf.select :currency
Hello, I've got a form field like this:
<%= search_form_for @q, :url => dashboard_index_path do |f| %>
<%= f.grouping_fields @q do |f| %>
<%= f.hidden_field_tag :or, 'or' %>
<%= f.text_field :status_signal_strength_gt %>
<%= f.text_field :status_signal_strength_lt %>
<%= f.text_field :status_battery_level_gt %>
<%= f.text_field :status_battery_level_lt %>
<%= f.text_field :status_sd_card_gt %>
<%= f.text_field :status_sd_card_lt %>
<%= f.text_field :status_application_gt %>
<%= f.text_field :status_application_lt %>
<%= f.text_field :status_available_ram_mem_gt %>
<%= f.text_field :status_available_ram_mem_lt %>
<% end %>
<%= f.grouping_fields @q do |f| %>
<%= f.hidden_field_tag :any, 'any' %>
<%= f.select :status_network_name %>
<%= f.text_field :status_network_type %>
<%= f.text_field :gadget_name %>
<%= f.text_field :gadget_model_name %>
<%= f.text_field :gadget_model_type_name %>
<% end %>
<%= f.submit %>
@q is associatiated with telepone, which has a status.
Status schema is:
create_table "statuses", :force => true do |t|
t.integer "company_id"
t.integer "telephone_id"
t.decimal "battery_level"
t.datetime "created_at"
t.datetime "updated_at"
t.string "os"
t.string "os_version"
t.string "imei"
t.string "imsi"
t.string "network_type"
t.string "network_name"
t.integer "lac"
t.string "app_version"
t.integer "signal_strength"
t.integer "available_ram_mem"
t.integer "total_application_storage", :limit => 8
t.integer "available_application_storage", :limit => 8
t.integer "total_sd_card_storage", :limit => 8
t.integer "uptime", :limit => 8
t.integer "available_sd_card_storage", :limit => 8
t.integer "sd_card"
t.integer "application"
end
But I get No valid predicate for status_network_name
when trying to submit the form. What am I doing wrong?
Ransack 0.4.1
Looks like the parenthesis are not surrounding their statements properly
Ernie - Ransack works really well, and we're happy with it.
We want to search on locations that are geocoded. This is using the geocoder gem for rails 3. rubygeocoder.com
Geocoder already has a scope "Near" that we were able to use prior to migrating to Ransack.
Do you know of a way using geocoder's "near" scope with the Ransack functionality?
Index.html:
= search_form_for @q do |f|
%h3
Search:
= f.label :category_id
%br
= f.collection_select :category_id_eq, Category.all, :id, :name, :include_blank => true
%br
= f.label "Sub-Category"
%br
= f.collection_select :subcategory_id_eq, Subcategory.all, :id, :name, :include_blank => true, :prompt => "select category!"
%br
= f.label "Contains"
%br
= f.text_field :title_or_details_cont
%br
= f.label "Within 50 miles of?"
%br
= f.text_field :location_eq
%br
= f.submit
controller.rb
@q = Job.search(params[:q])
@jobs = @q.result(:distinct => true)
Hi,
How can I use formtastic to render the search form? It doesn't work if I just swap out search_form_for with semantic_form_for.
Thanks,
Ernie, first of all thank you for your amazing gem! It's absolutely great!!!
I have a problem with generating a proper sql for searching when a model has a has_one through relation. The situation is as follows:
class Transfer < ActiveRecord::Base
belongs_to :event
belongs_to :location
belongs_to :person
belongs_to :site_start, :class_name => 'Site', :foreign_key => 'site_start_id'
belongs_to :site_end, :class_name => 'Site', :foreign_key => 'site_end_id'
has_one :egon_category, :through => :person
has_one :egon_sub_category, :through => :person
end
class Person < ActiveRecord::Base
belongs_to :event
belongs_to :egon_category
belongs_to :egon_sub_category
end
class EgonCategory < ActiveRecord::Base
belongs_to :event
has_many :egon_sub_categories, :dependent => :destroy
has_many :people
end
class EgonSubCategory < ActiveRecord::Base
belongs_to :event
belongs_to :egon_category
has_many :people
end
I omitted class definitions that are not relevant just so it's more readable. The sql generated by ransach:
Transfer.search(params[:q]).result.count
with
params[:q] = nil
is as follows:
SELECT COUNT(DISTINCT "transfers"."id") FROM "transfers" INNER JOIN
"locations" ON "locations"."id" = "transfers"."location_id" INNER JOIN
"people" ON "people"."id" = "transfers"."person_id" INNER JOIN
"events" ON "events"."id" = "transfers"."event_id" LEFT OUTER JOIN
"sites" ON "sites"."id" = "transfers"."site_start_id" LEFT OUTER JOIN
"sites" "site_ends_transfers" ON "site_ends_transfers"."id" = "transfers"."site_end_id" LEFT OUTER JOIN
**1"people" "egon_categories_transfers_join" ON "transfers"."id" = "egon_categories_transfers_join"."person_id" LEFT OUTER JOIN **
"egon_categories" ON "egon_categories"."id" = "egon_categories_transfers_join"."egon_category_id" LEFT OUTER JOIN
**2"people" "egon_sub_categories_transfers_join" ON "transfers"."id" = "egon_sub_categories_transfers_join"."person_id" LEFT OUTER JOIN **
"egon_sub_categories" ON "egon_sub_categories"."id" = "egon_sub_categories_transfers_join"."egon_sub_category_id"
there are 2 errors (i've put into the sql code *_{number}). The lines should look as follows:
for *_1:
"people" "egon_categories_transfers_join" ON "transfers"."person_id" = "egon_categories_transfers_join"."id" LEFT OUTER JOIN
for **2
"people" "egon_sub_categories_transfers_join" ON "transfers"."person_id" = "egon_sub_categories_transfers_join"."id" LEFT OUTER JOIN
Is there anything i can do to correct this error? Or maybe i'm doing something wrong.
Thank you in advance for any help!
Have a great day!
hi,
i'm back
I have a array with number in a DB column string type : ref1 [1,2,3,5,20] and other ref2 [1,4,9,15,20]
e.g., if I make a search with _cont = 2, it found ref1 and ref2, but is not good for me.
I try eq, matches ... but no answer.
which method can find the good data with array of numeric values ? Ransack can make a "split" and evaluate it like number.
Failing test for null / not_null predicate.
Expected null to generate "IS NULL" and not_null to generate "IS NOT NULL"
Currently returns "= NULL" and "!= NULL" respectively
Failing tests:
jeffreyiacono@ce0318c
Hello,
I want to create a custom predicate that uses a custom Arel predicate I have written that does case insensitive matching against a list of items.
My test looks like:
a = Factory.create(:mi_attempt, :colony_name => 'MAAB')
b = Factory.create(:mi_attempt, :colony_name => 'MAAN')
assert_equal [a, b],
MiAttempt.search(:colony_name_ci_in => ['maab', 'maan']).result
The configuration code looks like:
Ransack.configure do |config|
config.add_predicate 'ci_in',
:arel_predicate => 'ci_in'
end
I already have my 'ci_in' Arel predicate working (http://github.com/i-dcc/imits/blob/extjs4/config/initializers/arel_extensions.rb).
Looking at your code, you only allow arrays in for a hard-coded list of Arel predicates (currently 'in' and 'not_in'). I'm forking ransack to hard code my predicates in too, but can you think of another way to achieve this? Perhaps a config option saying whether or not it takes an array of arguments? (to set the @wants_array flag in the Predicate class, perhaps?)
Thanks
Asfand
model CLINIC has many TREATMENTS
I'm doing an advanced search on the clinics to filter them. I have this in my form:
<%= check_box_tag "q[treatments_id_eq][]", treatment.id, {} %>
Basically, i'm trying to send an array of the selected treatments to the search. I get the following error:
undefined method `to_i' for ["1", "2"]:Array
Using treatments_id_eq_ANY works and this is the generated query:
SELECT DISTINCT "clinics".* FROM "clinics" LEFT OUTER JOIN "countries" ON "countries"."id" = "clinics"."country_id" LEFT OUTER JOIN "clinics_treatments" ON "clinics_treatments"."clinic_id" = "clinics"."id" LEFT OUTER JOIN "treatments" ON "treatments"."id" = "clinics_treatments"."treatment_id" WHERE (("treatments"."id" = 1 OR "treatments"."id" = 2))
However I want to have them connected by AND instead of OR (WHERE (("treatments"."id" = 1 OR "treatments"."id" = 2)))
Hi , Ransack is awesome!
im using it for a proyect, im trying to make some category filters that use a collection in a checkbox (im using formtastic)
= f.input :category_name_eq, :collection=>Estate::Category.all.map(&:name), :as=>:check_boxes
so Ransack params:
...&q[category_name_eq][]=Sleep&q[category_name_eq][]=Use....
but in the query Ransack generates:
.... AND (("estate_categories"."name" = 'SleepUse' AND.....
it should be:
AND (("estate_categories"."name" = 'Sleep' or "estate_categories"."name" = 'Use' AND .....
how can accompish that ?
I'm converting a personal rails app from Rails 2.3 to Rails 3.
It's currently using search logic and I want to convert to either ransack or meta_search.
I'm using a search title_like_all 'won', 'war' which in search logic expands to
name LIKE '%won%' AND name LIKE '%war%'
that is both fred and johnson have to appear in the column value to satisfy the query.
I've tried this
Ransack.configure do |config|
config.add_predicate 'like', # Name your predicate
:arel_predicate => 'matches',
:formatter => proc {|v| v.split(/\s+/).map {|word| "%#{word}%"}},
:validator => proc {|v| v.present?},
:compounds => true,
:type => :string
end
But I get this where clause
WHERE (("movies"."title" LIKE '%won%', '%war%'))
which is an invalid sql expression.
Is there a way to do this?
Hi ernie,
I've been a happy user of meta_search and now ransack.
What I want is a select box with multiple select possibilities.
I have "f.select :level_in, Log.levels, { :include_blank => true, :multiple=>true,:size=>6 }"
But the :multiple and :size do not work. I don't know if they are supposed to work.
I would think that when choosing the predicate _in, you can select multiple options. Maybe this is a nice feature?
With kind regards,
Tom
Hi Ernie,
I am using Rails 3.1 and trying to implement a form that will use Ransack to lookup a person based on soundex values. Everything seems to work fine, I can get the correct records returned back to me, but the only way I have got this working, results in the users original input (for example someone searches for "joe) returned as the soundex value. I thought I could get around this using a hidden field or something but, then I still need a field for the user to input the name and any field I use gets included in the query. I gave more specifics here http://stackoverflow.com/questions/7926900/rails-how-to-not-evaluate-a-field-in-a-ransack-search-form . I don't actually think there is anything wrong with your gem but I can't work out how to implement it in this case. I apologize if this is not the right place to ask, but if you could point me in the right direction that would be appreciated. Thanks. Dale
Sort_link was a pretty cool way to sort result in metasearch.
Ransack can do something easy like this ?
Sorry to disturb and sorry for my bad english.
I have this search form:
= form_for @search, :url => intra_path do |f|
%p
= f.label :company_name_contains, t('company_name')
%br
= f.text_field :company_name_contains
%br
%br
%p
= f.label :vat_number_contains, t('vat_number')
%br
= f.text_field :vat_number_contains
= f.label :categories_id_equals, t('category')
= f.collection_select :categories_id_equals, @categories, :id, :name,
Say that I want to search for company_name "abc" and there is only one.
With meta_search I have one result.
Company has five categories associated, if I search company_name "abc" using ransack it returns five results, that is wrong.
I have a finder_sql relationship created in a model and when searching upon that relationship a foreign_key is trying to be added to the query...
Here is the model:
class Account
has_many :activities, :class_name => "Check", :finder_sql => proc { "SELECT * FROM checks
LEFT OUTER JOIN checking_accounts
ON checking_accounts
.id = checks
.maker_account_id
OR checking_accounts
.id = checks
.payee_account_id WHERE (checking_accounts.account_id = #{id})" },
:foreign_key => false
end
here is the search in the controller:
class CheckingAccountsController < ApplicationController
def filter_activities
@search = current_account.activities.search(params[:q])
end
end
Here is the bad sql:
ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'checks.account_id' in 'where clause': SELECT checks
.* FROM checks
WHERE checks
.account_id
= 11 AND checks
.maker_account_id
IN (1)):
As you can see, nowhere in my relationship am I querying the checks.account_id column...because it doesn't exist. Calling the relationship without searching works just fine, so I have to assume this is a problem with Ransack since that is what is performing the search
here is the situation.
I have a Company model with an attribute CLASSIFICATIONS = ["<= € 150.000,00"," > € 150.000,00 <= € 309.873,60"," > € 309.873,60 < € 500.000,00"].freeze.
In my seeds.rb I've put
a = ["<= € 150.000,00", "> € 150.000,00 <= € 309.873,60", "> € 150.000,00 <= € 309.873,60"]
for i in(1..100)
Company.create!(:name => "company-#{i}", :classification => a[rand(a.size)])
Then in the search form I've set:
= f.label :classification_eq, t('classification')
= f.select :classification_eq, Company::CLASSIFICATIONS, :include_blank => true
If I search for <= € 150.000,00 then is displayed a list of companies.
But if I search for > € 150.000,00 <= € 309.873,60 or > € 150.000,00 <= € 309.873,60 I have no list but there are companies with classification = <= € 309.873,60 or > € 150.000,00 <= € 309.873,60.
Here is my ransacker definition:
ransacker :price_per_hour_usd, type: :integer,
formatter: lambda { |v| v.to_i * 100 },
validator: lambda { |v| v.present? && v.to_i > 0 } do |parent|
parent.table[:price_per_hour_usd_cents]
end
It appears the validator
option has no effect. Is it possible to ignore negative/zero values for a ransacker?
How can I make a ransacker which will find records while calling DATE_FORMAT sql function?
I had this done on MetaWhere 1.x like this:
scope :calldate_day, lambda {|p|
p = "%02d" % p
where("DATE_FORMAT(calldate, '%d')='#{p}'")
}
scope :calldate_month, lambda {|p|
p = "%02d" % p
where("DATE_FORMAT(calldate, '%m')='#{p}'")
}
scope :calldate_year, lambda {|p|
where("DATE_FORMAT(calldate, '%Y')='#{p}'")
}
search_methods :calldate_day, :calldate_month, :calldate_year
but have no idea how to do this with ransacker method
Hi,
I'm using Squeel with Ransack and when I activate Ransack I get this error:
ActiveRecord::ConfigurationError: Association named 'supplier' was not found; perhaps you misspelled it?
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/ransack-0.5.2/lib/ransack/adapters/active_record/join_dependency.rb:34:in `build_polymorphic'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/ransack-0.5.2/lib/ransack/adapters/active_record/join_dependency.rb:21:in `block in graft_with_ransack'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/ransack-0.5.2/lib/ransack/adapters/active_record/join_dependency.rb:19:in `each'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/ransack-0.5.2/lib/ransack/adapters/active_record/join_dependency.rb:19:in `graft_with_ransack'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/squeel-0.8.4/lib/squeel/adapters/active_record/relation.rb:147:in `build_join_dependency'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/squeel-0.8.4/lib/squeel/adapters/active_record/relation.rb:76:in `build_arel'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/relation/query_methods.rb:167:in `arel'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:394:in `to_sql'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/relation/finder_methods.rb:205:in `find_with_associations'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:109:in `to_a'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/relation.rb:423:in `inspect'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
from /Users/mikz/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I have this models:
class Item < ActiveRecord::Base
belongs_to :category, :class_name => 'ItemCategory', :foreign_key => 'category_id'
belongs_to :supplier
end
class ItemCategory < ActiveRecord::Base
has_many :items, :foreign_key => 'category_id'
end
class Supplier < ActiveRecord::Base
has_many :items
has_many :item_categories, :through => :items
end
And I'm executing query:
ItemCategory.includes{items.supplier}.order{items.id}
What can I do? When I try this in squeel console with loaded ransack it works.
Ernie,
(Sorry for the long post, but I want to make sure you have all the information you need).
Given the following:
#person.rb
class Person < ActiveRecord::Base
end
#customer.rb
class Customer < Person
has_many :orders, :as => :customer
# I know it looks like I don't need the polymorphic association, but I have other classes that
# can also be customers, like InstitutionalCustomer, etc.
end
# order.rb
class Order < ActiveRecord::Base
belongs_to :customer, :polymorphic => true
end
When I try to search customer's emails I use (it's haml, but pretty simple markup):
= search_form_for [:admin, @q], :remote => true do |form|
%p
= form.label :first_name_or_last_name_cont, "Name cont"
= form.text_field :first_name_or_last_name_cont
%p
= form.label :email_cont
= form.text_field :email_cont
%p
= form.label :primary_phone_cont, "Phone"
= form.text_field :primary_phone_or_shipping_addresses_phone_or_billing_addresses_phone_cont
%p
= form.submit 'Search', :class => 'primary button'
That works just fine. However, when I try to search for orders by customer's emails:
= search_form_for [:admin, @q], :remote => false do |f|
%p
= f.label :customer_of_Person_type_first_name, 'First Name'
= f.text_field :customer_of_Person_type_first_name_cont
%p
= f.label :customer_of_Person_type_last_name, 'Last Name'
= f.text_field :customer_of_Person_type_last_name_cont
%p
= f.label :customer_of_Person_type_email_cont, 'Email'
= f.text_field :customer_of_Person_type_email_cont
%p
= f.submit 'Search', :class => 'primary button'
It results in an ActiveRecord::EagerLoadPolymorphicError (Can not eagerly load the polymorphic association :customer)
This happens when I use a search term that includes a '.' character (for example [email protected]). However if I use a term without the '.' then we do just fine (for example dan@dolphins).
Note that this only happens when searching for orders through the customer polymorphic association. If searching for customers only, the terms with '.' work just fine.
Thank you for your help and the gem (I am really making a whole lot of use both from ransack and squeel. Even using valium a bit now! Thanks again).
Is there any equivalent of the meta_search query below in ransack?
@search = current_user.projects.search(params[:search])
I am trying this:
@q = current_user.projects.search(params[:q])
But I get an undefined search method, I assume because it's an array:
undefined method `search' for #<Array:0x007f94c6c53d18>
Say I have a nested resource defined in my routes like
http://accounts.dev/projects/2/expenses
I want to the sort_link to link to append the querystring to the full nested url
http://accounts.dev/projects/2/expenses?q%5Bs%5D=reference+asc
but instead it links to the child resource
http://accounts.dev/expenses?q%5Bs%5D=reference+asc
Apologies if I'm missing something obvious.
@project = Project.find(params[:project_id]) @q = @project.expenses.search(params[:q])
<%= sort_link @q, :reference %>
Hi there,
The CoffeeScript in the Ransack demo conflicts with some code in the Cocoon gem, FYI: http://rubygems.org/gems/cocoon
var new_content = content.replace(regexp_braced, '[' + new_id + ']');
Renaming add_fields to add_s_fields is a quick workaround.
I'd like to do this:
f.hidden_combinator :any
This would be great for forms where you know the way you want the user to interact, but do not want to use hidden_field_tags all over.
I'm porting a very simple MetaSearch implementation to Ransack and I have a couple scopes I was using previously, but I can't seem to find a way to get Ransack to pick them up. :(
There's nothing on this in the readme.
How do I go about using scopes with Ransack?
Given a developer has Person and Article models
When he/she uses Ransack on Person, but searches for people who have published articles on the last 30 days
Then he/she should be able to retrieve Articles on the last 30 days without additional scoping
Right now, I have to find the appropriate param, see if it's present, and then use it on scopes. person.articles just fetches all the associated articles.
With ransack I can search for a date in a date input field, so I can search for example for 2011-02-02. How can I search only for year?
Ernie,
As I need to customize my ransack attribute select for an advanced search form, I started down the path of constructing a custom select helper. I've got it all working with the exception of persisting the selected attribute after post:
<%= select f.object_name, "name", options_for_select(['...','...'])... %>
Obviously it's a hack but it does work for the most part without me surrendering control to your attribute_select helper which I can't customize. Writing out the params hash after post clearly shows the nested value, but I can't seem to find a simple solution to persisting the selection after submit. I tried reverse-engineering your code, but you're using different helpers on the object. I've tinkered around with variations on :selected => params[f.object_name] without success. Any thoughts?
Thanks,
Jason
Hi Ernie,
I'm trying to move a search form into my website header using a partial. It works ok unless I switch to another controller while browsing the site. Do I need to pass anything extra into my search_form_for in order to render outside its related controller? As you can see I'm fairly new to rails.
Thanks for your help.
ArgumentError in Videos#show
Showing /Users/x/Documents/Development/webdev/x/app/views/application/_search.html.erb where line #2 raised:
No Ransack::Search object was provided to search_form_for!
Extracted source (around line #2):
1:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.