Abstract away use of HABTM macro
By having the `:has_and_belongs_to_many` macro in the `@collection` we are punishing `:has_many` associations because it has to allocate the array and check the macro. @collection is returned to `macro == :has_many` and a new reflection class `HABTMReflection` is created to handle this case instead.
This commit is contained in:
parent
66eb847c9d
commit
9eb87fbf15
@ -1577,7 +1577,7 @@ def has_and_belongs_to_many(name, scope = nil, options = {}, &extension)
|
|||||||
scope = nil
|
scope = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
habtm_reflection = ActiveRecord::Reflection::AssociationReflection.new(:has_and_belongs_to_many, name, scope, options, self)
|
habtm_reflection = ActiveRecord::Reflection::HABTMReflection.new(:has_and_belongs_to_many, name, scope, options, self)
|
||||||
|
|
||||||
builder = Builder::HasAndBelongsToMany.new name, self, options
|
builder = Builder::HasAndBelongsToMany.new name, self, options
|
||||||
|
|
||||||
|
@ -228,7 +228,7 @@ def klass
|
|||||||
|
|
||||||
def initialize(macro, name, scope, options, active_record)
|
def initialize(macro, name, scope, options, active_record)
|
||||||
super
|
super
|
||||||
@collection = [:has_many, :has_and_belongs_to_many].include?(macro)
|
@collection = macro == :has_many
|
||||||
@automatic_inverse_of = nil
|
@automatic_inverse_of = nil
|
||||||
@type = options[:as] && "#{options[:as]}_type"
|
@type = options[:as] && "#{options[:as]}_type"
|
||||||
@foreign_type = options[:foreign_type] || "#{name}_type"
|
@foreign_type = options[:foreign_type] || "#{name}_type"
|
||||||
@ -538,6 +538,13 @@ def primary_key(klass)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class HABTMReflection < AssociationReflection #:nodoc:
|
||||||
|
def initialize(macro, name, scope, options, active_record)
|
||||||
|
super
|
||||||
|
@collection = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Holds all the meta-data about a :through association as it was specified
|
# Holds all the meta-data about a :through association as it was specified
|
||||||
# in the Active Record class.
|
# in the Active Record class.
|
||||||
class ThroughReflection < AssociationReflection #:nodoc:
|
class ThroughReflection < AssociationReflection #:nodoc:
|
||||||
|
Loading…
Reference in New Issue
Block a user