Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class ActiveRecord::Base

  def self.unprotected_create!(*args)
    with_unprotected_attributes { create!(*args) }
  end
  
  def self.unprotected_create(*args)
    with_unprotected_attributes { create(*args) }
  end
  
  def self.unprotected_new(*args)
    with_unprotected_attributes { new(*args) }
  end
  
protected

  def self.with_unprotected_attributes(&block)
    previous_attr_protected  = read_inheritable_attribute("attr_protected")
    previous_attr_accessible = read_inheritable_attribute("attr_accessible")

    write_inheritable_attribute("attr_protected",  nil)
    write_inheritable_attribute("attr_accessible", nil)

    result = yield

    write_inheritable_attribute("attr_protected",  previous_attr_protected)
    write_inheritable_attribute("attr_accessible", previous_attr_accessible)

    result
  end
  
end