筛选:字符串无法在Active Admin上运行(Rails 3.2 / postgresql)

时间:2014-03-01 16:51:38

标签: ruby-on-rails ruby ruby-on-rails-3 postgresql activeadmin

我在每日交易应用中使用Active Admin。

我遇到了一个非常具体的问题:当我在定义为filter :as =>:string的字段上的过滤器侧边栏上启动搜索时,出现此错误:

enter image description here

ActiveRecord::StatementInvalid at /admin/users
PG::UndefinedFunction: ERROR:  operator does not exist: character varying >> unknown
LINE 1: ...ECT COUNT(*) FROM "users"  WHERE ("users"."email" >> 'math')
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(*) FROM "users"  WHERE ("users"."email" >> 'math')

奇怪的是,如果我选择,而不是'包含'等于或'以'开头'或'结束',它可以完美地运作。我只对'contains'有疑问。

就像postgresql没有对'包含'的字符串上的命令Active Admin过滤器请求他做。

我尝试了很多模特,而且我的所有模特都在使用它。

以下是Active Admin页面:

ActiveAdmin.register People do
  filter :name, :as => :string
end

这是我的宝石文件

gem 'activeadmin',            github: 'gregbell/active_admin' # added github because of 
gem 'pg'

以下是我在当地获得的完整内容:

Started GET "/admin/users?utf8=%E2%9C%93&q%5Bemail_contains%5D=math&commit=Filter&order=id_desc" for 127.0.0.1 at 2014-03-01 17:45:42 +0100
Processing by Admin::UsersController#index as HTML
  Parameters: {"utf8"=>"✓", "q"=>{"email_contains"=>"math"}, "commit"=>"Filter", "order"=>"id_desc"}
  AdminUser Load (1.4ms)  SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 1 ORDER BY "admin_users"."id" ASC LIMIT 1
   (2.3ms)  SELECT COUNT(*) FROM "users" WHERE ("users"."email" >> 'math')
**PG::UndefinedFunction: ERROR:  operator does not exist: character varying >> unknown
LINE 1: ...ECT COUNT(*) FROM "users"  WHERE ("users"."email" >> 'math')
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.**
: SELECT COUNT(*) FROM "users"  WHERE ("users"."email" >> 'math')
  Rendered /home/mat/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/bundler/gems/active_admin-66de2f86ae1d/app/views/active_admin/resource/index.html.arb (32.7ms)
Completed 500 Internal Server Error in 47ms

ActiveRecord::StatementInvalid - PG::UndefinedFunction: ERROR:  operator does not exist: character varying >> unknown
LINE 1: ...ECT COUNT(*) FROM "users"  WHERE ("users"."email" >> 'math')
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(*) FROM "users"  WHERE ("users"."email" >> 'math'):
  activerecord (4.0.3) lib/active_record/connection_adapters/abstract_adapter.rb:440:in `rescue in log'
  activerecord (4.0.3) lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
  activerecord (4.0.3) lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
  activerecord (4.0.3) lib/active_record/connection_adapters/postgresql_adapter.rb:891:in `select'
  activerecord (4.0.3) lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
  activerecord (4.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `block in select_all'
  activerecord (4.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:76:in `cache_sql'
  activerecord (4.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:61:in `select_all'
  activerecord (4.0.3) lib/active_record/relation/calculations.rb:262:in `execute_simple_calculation'
  activerecord (4.0.3) lib/active_record/relation/calculations.rb:224:in `perform_calculation'
  activerecord (4.0.3) lib/active_record/relation/calculations.rb:108:in `calculate'
  activerecord-deprecated_finders (1.0.3) lib/active_record/deprecated_finders/relation.rb:84:in `calculate'
  activerecord (4.0.3) lib/active_record/relation/calculations.rb:24:in `count'
   () home/mmat/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/bundler/gems/active_admin-66de2f86ae1d/lib/active_admin/helpers/collection.rb:11:in `collection_size'
   () home/mmat/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/bundler/gems/active_admin-66de2f86ae1d/lib/active_admin/views/components/scopes.rb:62:in `get_scope_count'
   () home/mmat/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/bundler/gems/active_admin-66de2f86ae1d/lib/active_admin/views/components/scopes.rb:40:in `block (3 levels) in build_scope'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:31:in `block in build_tag'
  arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:18:in `span'
   () home/mmat/.rvm/gems/ruby-1.9.3-p392@rails3tutorial2ndEd/bundler/gems/active_admin-66de2f86ae1d/lib/active_admin/views/components/scopes.rb:39:in `block (2 levels) in build_scope'
  arbre (1.0.1) lib/arbre/element/builder_methods.rb:31:in `block in build_tag'


Started POST "/__better_errors/70228353699700/variables" for 127.0.0.1 at 2014-03-01 17:45:43 +0100
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE ("users"."email" >> 'math') ORDER BY "users"."id" desc
PG::UndefinedFunction: ERROR:  operator does not exist: character varying >> unknown
LINE 1: ...CT "users".* FROM "users"  WHERE ("users"."email" >> 'math')...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "users".* FROM "users"  WHERE ("users"."email" >> 'math')  ORDER BY "users"."id" desc

修改

我不知道它是否与问题有关,或者它是否有帮助,但由于Active Admin使用Ransack,我认为在我的Ransack初始化程序中编写的内容可能很有用

Ransack.configure do |config|
  %w[contained_within contained_within_or_equals contains contains_or_equals overlap].each do |p|
    config.add_predicate p, arel_predicate: p, wants_array: true
  end
end

2 个答案:

答案 0 :(得分:0)

  

就像postgresql没有对'包含'的字符串上的命令Active Admin过滤器请求他做。

是的,并从错误中检查这些行:

PG::UndefinedFunction: ERROR: operator does not exist: character varying >> unknown LINE 1: ...ECT COUNT(*) FROM "users" WHERE ("users"."email" >> 'math')

Postgres被赋予运营商>>以在users表中的email列和搜索字符串'math'之间使用。可以使用哪些运算符取决于要查询的列的数据类型,您可以在数据库CLI中调查这些数据类型。在命令行中输入rails dbconsole并检查我认为您可以使用的列的数据类型:

select * from INFORMATION_SCHEMA.COLUMNS

电子邮件通常存储为字符串。如果是这种情况,使用>>没有意义,因为它没有出现在the Postgres documentation's list of operators for strings中。反过来,这表明ActiveAdmin或其中一个依赖项误解了您的contains

顺便说一句,看起来你使用:as =>:string过滤的一些属性,比如Person.name,在你处理它们的级别上可能是ruby StringActive Admin's documentation seems to suggest those attributes' filter input elements would default to text fields;那是这样吗?如果你还没有,那么值得尝试删除它们,虽然大概应该没关系。

答案 1 :(得分:0)

我很想低头看看"构建一个自定义过滤器,该过滤器的调用代码我知道可以正常使用#34;路线。我可能首先看看这两个解决方案,看看我是否可以一起使用它们来制作可用的东西:

https://github.com/gregbell/active_admin/issues/45 - "创建自定义过滤器" https://github.com/textacular/textacular - postgresql的全文搜索gem

似乎有一个非常简单的解决方案,您可以使用textacular创建自定义范围,然后通过简单的自定义过滤器将其绑定到ActiveAdmin。