Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2009: associations_dependent_1.diff

File associations_dependent_1.diff, 2.7 kB (added by robbyrussell, 3 years ago)

association.rb diff

  • associations.rb

    old new  
    263263      # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name 
    264264      #   of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_many association will use "person_id" 
    265265      #   as the default foreign_key. 
    266       # * <tt>:dependent</tt>   - if set to true all the associated object are destroyed alongside this object. 
     266      # * <tt>:dependent</tt>   - if set to :destroy (or true) all the associated objects are destroyed alongside this object. Also accepts :nullify 
     267      #   which will set the associated objects foriegn key field to NULL.  
    267268      #   May not be set if :exclusively_dependent is also set. 
    268269      # * <tt>:exclusively_dependent</tt>   - if set to true all the associated object are deleted in one SQL statement without having their 
    269270      #   before_destroy callback run. This should only be used on associations that depend solely on this class and don't need to do any 
     
    276277      # 
    277278      # Option examples: 
    278279      #   has_many :comments, :order => "posted_on" 
     280      #   has_many :comments, :dependent => :nullify 
    279281      #   has_many :people, :class_name => "Person", :conditions => "deleted = 0", :order => "name" 
    280       #   has_many :tracks, :order => "position", :dependent => true 
     282      #   has_many :tracks, :order => "position", :dependent => :destroy 
    281283      #   has_many :subscribers, :class_name => "Person", :finder_sql => 
    282284      #       'SELECT DISTINCT people.* ' + 
    283285      #       'FROM people p, post_subscriptions ps ' + 
     
    296298        # See HasManyAssociation#delete_records.  Dependent associations 
    297299        # delete children, otherwise foreign key is set to NULL. 
    298300        elsif options[:dependent] 
    299           module_eval "before_destroy '#{association_name}.each { |o| o.destroy }'" 
     301          case options[:dependent] 
     302            when :destroy, true   
     303              module_eval "before_destroy '#{association_name}.each { |o| o.destroy }'" 
     304            when :nullify 
     305              module_eval "before_destroy { |record| #{association_class_name}.update_all(%(#{association_class_primary_key_name} = NULL),  %(#{association_class_primary_key_name} = \#{record.quoted_id})) }" 
     306            else 
     307              raise ArgumentError, 'The :dependent option expects either :destroy or :nullify'  
     308          end 
    300309        elsif options[:exclusively_dependent] 
    301310          module_eval "before_destroy { |record| #{association_class_name}.delete_all(%(#{association_class_primary_key_name} = \#{record.quoted_id})) }" 
    302311        end