B. *_*tty 2 ruby rubygems ruby-on-rails ransack
我的代码一直在运行并工作,直到我安装了 gem“ipaddress”,但现在我之前创建的模型都出现以下错误
Ransack 需要将 <MODEL_NAME> 关联明确列入可搜索许可名单。
在模型中定义一个ransackable_associations类方法User
,注意您不希望搜索的项目(例如,encrypted_password、password_reset_token或owner其他敏感信息)。您可以使用以下内容作为基础:
class User < ApplicationRecord
# ...
def self.ransackable_associations(auth_object = nil)
["comments", "created_by", "favorites", "impressions_by_user", "updated_by", "user_group"]
end
# ...
The issue I have with this is I need a dynamic to do this instead of having to redo this for every single model that I have created.
Run Code Online (Sandbox Code Playgroud)
我必须将模型从使用 ActiveRecord::Base 更改为使用 ApplicationRecord,然后创建一个包含以下代码的 Application_Record.rb 文件:
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
Rails.logger.info("INSIDE APPLICATION RECORD");
self.abstract_class = true
def self.ransackable_attributes(auth_object = nil)
column_names + _ransackers.keys
end
def self.ransackable_associations(auth_object = nil)
reflect_on_all_associations.map { |a| a.name.to_s } + _ransackers.keys
end
end
Run Code Online (Sandbox Code Playgroud)
然后必须在初始化程序中调整印象派扩展以添加以下方法
def ransackable_associations(auth_object = nil)
Rails.logger.info("WITHIN RANSACK ASSOCIATION")
super + %w[impressionable]
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3991 次 |
| 最近记录: |