Мне кажется, это что-то, что можно установить прямо из кода вашего контроллера в классе для этого запроса.Например,
Employee.accessible = :all
Company.create(params[:company])
Employee.accessible = nil
Который может быть извлечен в блок типа
def with_accessible(*types)
types.flatten!
types.each{|type| type.accessible = :all}
yield
types.each{|type| type.accessible = nil}
end
Итак, ваш окончательный код контроллера равен
with_accessible(Employee, OtherClass, YetAnotherClass) do
Company.create(params[:company])
end
Довольно выразительно, что происходитрегистр всех атрибутов
Для случая только определенных атрибутов я мог бы изменить его на следующий
def with_accessible(*types, &block)
types.flatten!
return with_accessible_hash(types.first, &block) if types.first.is_a?(Hash)
types.each{|type| type.accessible = :all}
ret = yield
types.each{|type| type.accessible = nil}
ret
end
def with_accessible_hash(hash, &block)
hash.each_pair do |klass, accessible|
Object.const_get(klass).accessible = accessible
end
ret = yield
hash.keys.each{|type| type.accessible = nil}
ret
end
, что дает вам
with_accessible(:Employee => [:a, :b, :c], :OtherClass => [:a, :b]) do
Company.create(params[:company])
end