Search code examples
ruby-on-railsruby-on-rails-4activeadminhstore

ActiveAdmin Custom Scope with hstore


I am attempting to write a custom scope for ActiveAdmin, but keep running into errors:

  1. Search isn't performing
  2. I keep getting an error wrong number of arguments (0 for 1)

admin/user.rb

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

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?


Solution

  • So I was able to find out a solution for my question. Found this from: https://github.com/activerecord-hackery/ransack/issues/267#

    Heres what I did for the fix:

    admin/user.rb

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

    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. 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).