I'm struggling upgrading ransack from some 3.x version 4.0 where allowed ransackable attributes should be defined explicitly, I have fixed all project models, but yet now it is failing on rails core module.
# RuntimeError:
# Ransack needs ActionText::RichText attributes explicitly allowlisted as
# searchable. Define a `ransackable_attributes` class method in your `ActionText::RichText`
# model, watching out for items you DON'T want searchable (for
# example, `encrypted_password`, `password_reset_token`, `owner` or
# other sensitive information). You can use the following as a base:
#
# ```ruby
# class ActionText::RichText < ApplicationRecord
#
# # ...
#
# def self.ransackable_attributes(auth_object = nil)
# ["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
# end
#
# # ...
#
# end
# ```
I already tried to reopen class directly in sources, but that changes were not fetched by Rails and ignored. I tried to find some configs changes during initialization, but yet this is also not working. I bet someone already solved this migration from 3.0 to 4.x
app/models/action_text/rich_text.rb
class ActionText::RichText < ApplicationRecord
# ...
def self.ransackable_attributes(auth_object = nil)
["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
end
# ...
end
Result -> just ignored, same issue rails do not see those changes.
config/initializers/action_text.rb
class ActionText::RichText < ActiveRecord::Base
def self.ransackable_attributes(auth_object = nil)
["body", "created_at", "id", "locale", "name", "record_id", "record_type", "updated_at"]
end
end
Result error during init load ->
An error occurred while loading rails_helper.
Failure/Error: require File.expand_path('../config/environment', __dir__)
NoMethodError:
undefined method `has_many_attached' for ActionText::RichText:Class
# ./config/initializers/action_text.rb:7:in `<main>'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:6:in `require'
# ./spec/rails_helper.rb:6:in `<top (required)>'
No examples found.
Looks like you need make that log said — explicitly define suggested method. Since there is such inheritance: ActionText::RichText < ActionText::Record < ActiveRecord::Base
, you can define this method in the parent class of ActionText::RichText
# config/initializers/action_text.rb
class ActionText::Record < ActiveRecord::Base
def self.ransackable_attributes(auth_object = nil)
authorizable_ransackable_attributes
end
end
It is also possible to define this method in the initializer directly in the ActionText::RichText
class. But 'method_missing': undefined method 'has_many_attached' for ActionText::RichText:Class (NoMethodError)
(error from the question text) can be raised because of loading order (this DSL method is not available yet)
To avoid it, you can use ActiveSupport hook, its name can be found here
In this case patch will looks like this
# config/initializers/action_text.rb
ActiveSupport.on_load(:action_text_rich_text) do
class ActionText::RichText < ActionText::Record
def self.ransackable_attributes(auth_object = nil)
authorizable_ransackable_attributes
end
end
end
Of course instead of authorizable_ransackable_attributes
you can use explicit array of needed attributes (strings array) like %w[id body name record_id record_type]
, etc.