Ransack: Not working in rails 5

Created on 29 May 2017  路  11Comments  路  Source: activerecord-hackery/ransack

Is it working in rails 5? I can't get it to work

Most helpful comment

Thanks for your answer! I solved this problem by adding migration for Hash field. It replaced hash type from ActionController::Parameters to ActiveSupport::HashWithIndifferentAccess

def up
  execute("UPDATE user_channel_layouts SET data = REPLACE(data, '!ruby/hash:ActionController::Parameters', '!ruby/hash:ActiveSupport::HashWithIndifferentAccess')")
end

All 11 comments

Works same way as in r4. Check out the readme ;)

i found the problem too, Staff.ransack(params[:q]) not working in rails 5.1.1.

Are you on master?

1.8.3 has been released.

@vhilly @ajream Hi, did you add any CONSTRAINTs to search field ?

REF: https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching

Hi! I have problems with rails 5.1.1 and last ransack version (1.8.3) too.
This is my case:

data = UserChannelLayout.ransack(s: 'created_at desc').result
data.class
=> UserChannelLayout::ActiveRecord_Relation

data.inspect
=> UserChannelLayout Load (9.0ms)  SELECT  "user_channel_layouts".* FROM "user_channel_layouts" ORDER BY "user_channel_layouts"."created_at" DESC LIMIT $1  [["LIMIT", 11]]
ActiveRecord::SerializationTypeMismatch (can't load `data`: was supposed to be a Hash, but was a ActionController::Parameters. -- <ActionController::Parameters {"0"=><ActionController::Parameters {"channel_id"=>20062903, "supervision_id"=>48851} permitted: false>, "1"=><ActionController::Parameters {"channel_id"=>20062881, "supervision_id"=>48852} permitted: false>} permitted: false>)

channel_id and supervision_id are fields from UserChannelLayout model.

data = UserChannelLayout.ransack().result works fine.

UPD: UserChannelLayout model has text field data that converts to Hash by serialize :data, Hash

class UserChannelLayout < ApplicationRecord
  serialize :data, Hash
end

Sorry, my problem is not related with ransack. It is because part of my data has type hash:ActionController::Parameters instead of hash:ActiveSupport::HashWithIndifferentAccess and could not be serialized to Hash by serialize :data, Hash
wrong data example from database:

"--- !ruby/hash:ActionController::Parameters\n'0': !ruby/hash:ActionController::Parameters\n
channel_id: 20062903\n  supervision_id: 48851\n'1': !ruby/hash:ActionController::Parameters\n
channel_id: 20062881\n  supervision_id: 48852\n"

810 I hope it can help you

Thanks for your answer! I solved this problem by adding migration for Hash field. It replaced hash type from ActionController::Parameters to ActiveSupport::HashWithIndifferentAccess

def up
  execute("UPDATE user_channel_layouts SET data = REPLACE(data, '!ruby/hash:ActionController::Parameters', '!ruby/hash:ActiveSupport::HashWithIndifferentAccess')")
end

After updating to Rails 5 I have an errors like this:

forwarding to private method Ransack::Search#city_id_eq/.rvm/gems/ruby-2.4.1/gems/ransack-1.8.3/lib/ransack/helpers/form_builder.rb:10:in `value': ItemSearch#city_id

I think these are not Ransack issues. Can we close this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MatsumotoHiroko picture MatsumotoHiroko  路  4Comments

seanfcarroll picture seanfcarroll  路  4Comments

edgarbarrero picture edgarbarrero  路  4Comments

seanfcarroll picture seanfcarroll  路  3Comments

ivanovaleksey picture ivanovaleksey  路  3Comments