简体   繁体   English

具有hstore的ActiveAdmin自定义范围

[英]ActiveAdmin Custom Scope with hstore

I am attempting to write a custom scope for ActiveAdmin, but keep running into errors: 我正在尝试为ActiveAdmin编写自定义范围,但始终遇到错误:

  1. Search isn't performing 搜索效果不佳
  2. I keep getting an error wrong number of arguments (0 for 1) 我不断收到wrong number of arguments (0 for 1)

admin/user.rb 管理员/user.rb

filter :user_upload, label: 'User Upload Ability', as: :select, collection: [['On', 'false'], ['Off', 'true']]

user.rb user.rb

scope :user_upload, ->(value) { where('properties @> hstore(?, ?)', 'upload', value) }

def self.ransackable_scopes(auth_object = nil)
  :user_upload
end

Example User 示例用户

#<User id: 1, name: "Example", created_at: "2015-03-14 07:00:00", updated_at: "2016-04-13 20:27:50", properties: {"upload"=>"false"}>

Not sure if I am going about the the correct way. 不知道我是否要使用正确的方法。 Any ideas on how I can perform my scope so I can filter users by their upload property? 关于如何执行范围,以便可以根据users的上传属性过滤users的任何想法?

So I was able to find out a solution for my question. 因此,我能够为我的问题找到解决方案。 Found this from: https://github.com/activerecord-hackery/ransack/issues/267# 从以下位置找到这个: https : //github.com/activerecord-hackery/ransack/issues/267#

Heres what I did for the fix: 这是我为修复所做的事情:

admin/user.rb 管理员/user.rb

filter :upload_eq, label: 'User Upload Ability', as: :select, collection: { 'On' => 'false', 'Off' => 'true' }

user.rb user.rb

ransacker :upload do |parent|
      Arel::Nodes::InfixOperation.new('->', parent.table[:properties], Arel::Nodes.build_quoted('upload'))
end

Turns out I didn't need to use the ransackable_scopes method in order to achieve this. 事实证明,我不需要使用ransackable_scopes方法即可实现这一目标。 And because I am using Rails 4.2 I had to wrap build_quoted around the upload property because I was getting a unsupported: String error ( https://github.com/rails/arel/issues/323 ). 而且由于我使用的是Rails 4.2,我不得不在upload属性周围包裹build_quoted ,因为我得到了unsupported: String错误( https://github.com/rails/arel/issues/323 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM