我正在尝试使用 Ransack 实现 railscast风格的多条件搜索,并且使用多个选择作为属性的值有些困难/ predicate / value style search。
这个问题的关键似乎是下面的代码。尽我所能,我正在努力通过多种条件。我有什么方法可以使用这种方法吗?
如果无法做到这一点,您能否建议一种变通方法或替代方法?
代码:
q = {"c"=>{"0"=>{"a"=>{"0"=>{"name"=>"role_type_id"}}, "p"=>"in", "v"=>{"0"=>{"value"=>[5, 6]}}}}}
reload!; @q = StaffMember.ransack(q).result.count
错误:
NoMethodError: undefined method 'to_i' for [5, 6]:Array
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/nodes/value.rb:81:in 'cast_to_integer'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/nodes/value.rb:34:in 'cast'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/predicate.rb:74:in 'block in validate'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/predicate.rb:74:in 'each'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/predicate.rb:74:in 'any?'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/predicate.rb:74:in 'validate'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/nodes/condition.rb:48:in 'valid?'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/nodes/grouping.rb:44:in 'block in conditions='
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/nodes/grouping.rb:42:in 'each'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/nodes/grouping.rb:42:in 'conditions='
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/search.rb:51:in 'block in build'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/search.rb:47:in 'each'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/search.rb:47:in 'build'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/search.rb:39:in 'initialize'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/adapters/active_record/base.rb:15:in 'new'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/ransack-1.6.6/lib/ransack/adapters/active_record/base.rb:15:in 'ransack'
from (irb):421 from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in 'start'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in 'start'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in 'console'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in 'run_command!'
from /Users/sig/.rvm/gems/ruby-1.9.3-p484@sense-r4/gems/railties-4.2.1/lib/rails/commands.rb:17:in '<top (required)>'
from script/rails:6:in 'require' from script/rails:6:in '<main>'