我知道这是旧的,但我想分享一下我在你不希望在所有的车型UNRANSACKABLE_ATTRIBUTES事件处理这种方式。我创建了扩展的ActiveRecord的Ransacks模块:: Base的
module Ransack
module Adapters
module ActiveRecord
module Base
attr_accessor :_spare
def spare_ransack(*attribs)
self._spare = attribs.map{|a| a.to_s}
end
def spareables
self._spare ||= []
column_names - self._spare
end
def ransackable_attributes(auth_object = nil)
spareables + _ransackers.keys
end
end
end
end
end
一个初始化然后你可以使用
MyClass < ActiveRecord::Base
spare_ransack :id, :created_at, :updated_at, :section
end
这些属性不会ransackable。我喜欢这种方法,因为它可以让你有条件地设置它们。
编辑
虽然在为Class魔术这样做对于那些你的工作,不介意
def spare_ransack(*attribs)
self._spare = attribs.map do |a|
case a.to_sym
#remove time_stamp fields
when :time_stamps
["created_at","updated_at"]
#requires spare_ransack to be called after associations in the file
when :association_keys
reflect_on_all_associations.select{|a| a.macro == :belongs_to}.collect{|a| a.options[:foreign_key] || "#{a.name}_id"}
#remove primary key field
when :primary
primary_key
else
a.to_s
end
end.flatten
end
MyClass < ActiveRecord::Base
spare_ransack :primary,:time_stamps, :section
end
谢谢替代方法 – Berlin 2013-12-05 17:41:53
您的欢迎,虽然请知道这种方法是皱起了眉头皱了皱眉由厄尔(ransack的创造者)提供。我也扩大了实施见更新 – engineersmnky 2013-12-05 21:40:53