diff options
author | John Mileham <jmileham@gmail.com> | 2011-03-24 15:03:08 -0400 |
---|---|---|
committer | John Mileham <jmileham@gmail.com> | 2011-03-24 15:03:08 -0400 |
commit | b44a0ec153c84384e9b97a069e81f28bc5c2a4bf (patch) | |
tree | 94713aff0c241f91530dfd714bf685019c84ccab /activerecord/lib/active_record/associations/association.rb | |
parent | d5994ee48af14d67f0eec7d23863d4b19211b078 (diff) | |
parent | 5214e73850916de3c9127d35a4ecee0424d364a3 (diff) | |
download | rails-b44a0ec153c84384e9b97a069e81f28bc5c2a4bf.tar.gz rails-b44a0ec153c84384e9b97a069e81f28bc5c2a4bf.tar.bz2 rails-b44a0ec153c84384e9b97a069e81f28bc5c2a4bf.zip |
Merge branch 'master' of github.com:rails/rails into count_behavior
Diffstat (limited to 'activerecord/lib/active_record/associations/association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/association.rb | 52 |
1 files changed, 10 insertions, 42 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 86904ea2bc..27c446b12c 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -7,7 +7,7 @@ module ActiveRecord # This is the root class of all associations ('+ Foo' signifies an included module Foo): # # Association - # SingularAssociaton + # SingularAssociation # HasOneAssociation # HasOneThroughAssociation + ThroughAssociation # BelongsToAssociation @@ -88,28 +88,14 @@ module ActiveRecord # Construct the scope for this association. # - # Note that the association_scope is merged into the targed_scope only when the + # Note that the association_scope is merged into the target_scope only when the # scoped method is called. This is because at that point the call may be surrounded # by scope.scoping { ... } or with_scope { ... } etc, which affects the scope which # actually gets built. def construct_scope - @association_scope = association_scope if klass - end - - def association_scope - scope = klass.unscoped - scope = scope.create_with(creation_attributes) - scope = scope.apply_finder_options(options.slice(:readonly, :include)) - scope = scope.where(interpolate(options[:conditions])) - if select = select_value - scope = scope.select(select) + if klass + @association_scope = AssociationScope.new(self).scope end - scope = scope.extending(*Array.wrap(options[:extend])) - scope.where(construct_owner_conditions) - end - - def aliased_table - klass.arel_table end # Set the inverse association, if possible @@ -174,42 +160,24 @@ module ActiveRecord end end - def select_value - options[:select] - end - - # Implemented by (some) subclasses def creation_attributes - { } - end - - # Returns a hash linking the owner to the association represented by the reflection - def construct_owner_attributes(reflection = reflection) attributes = {} - if reflection.macro == :belongs_to - attributes[reflection.association_primary_key] = owner[reflection.foreign_key] - else + + if [:has_one, :has_many].include?(reflection.macro) && !options[:through] attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key] - if options[:as] - attributes["#{options[:as]}_type"] = owner.class.base_class.name + if reflection.options[:as] + attributes[reflection.type] = owner.class.base_class.name end end - attributes - end - # Builds an array of arel nodes from the owner attributes hash - def construct_owner_conditions(table = aliased_table, reflection = reflection) - conditions = construct_owner_attributes(reflection).map do |attr, value| - table[attr].eq(value) - end - table.create_and(conditions) + attributes end # Sets the owner attributes on the given record def set_owner_attributes(record) if owner.persisted? - construct_owner_attributes.each { |key, value| record[key] = value } + creation_attributes.each { |key, value| record[key] = value } end end |