Permalink
Browse files

define attribute methods in a thread safe manner

  • Loading branch information...
1 parent 4236d8f commit b670d799e4f7c1ddc235fbeec1ca1806a8b5d6ff @tenderlove tenderlove committed Jul 2, 2013
Showing with 4 additions and 5 deletions.
  1. +4 −5 activerecord/lib/active_record/attribute_methods.rb
@@ -37,11 +37,12 @@ def define_attribute_methods # :nodoc:
# Use a mutex; we don't want two thread simultaneously trying to define
# attribute methods.
generated_attribute_methods.synchronize do
- return if attribute_methods_generated?
+ return false if attribute_methods_generated?
superclass.define_attribute_methods unless self == base_class
super(column_names)
@attribute_methods_generated = true
end
+ true
end
def attribute_methods_generated? # :nodoc:
@@ -132,9 +133,7 @@ def attribute_names
# If we haven't generated any methods yet, generate them, then
# see if we've created the method we're looking for.
def method_missing(method, *args, &block) # :nodoc:
- unless self.class.attribute_methods_generated?
- self.class.define_attribute_methods
-
+ if self.class.define_attribute_methods
if respond_to_without_attributes?(method)
send(method, *args, &block)
else
@@ -177,7 +176,7 @@ def attribute_missing(match, *args, &block) # :nodoc:
# person.respond_to(:nothing) # => false
def respond_to?(name, include_private = false)
name = name.to_s
- self.class.define_attribute_methods unless self.class.attribute_methods_generated?
+ self.class.define_attribute_methods
result = super
# If the result is false the answer is false.

0 comments on commit b670d79

Please sign in to comment.