Ticket #2009: associations_dependent_1.diff
| File associations_dependent_1.diff, 2.7 kB (added by robbyrussell, 3 years ago) |
|---|
-
associations.rb
old new 263 263 # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name 264 264 # of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_many association will use "person_id" 265 265 # 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. 267 268 # May not be set if :exclusively_dependent is also set. 268 269 # * <tt>:exclusively_dependent</tt> - if set to true all the associated object are deleted in one SQL statement without having their 269 270 # before_destroy callback run. This should only be used on associations that depend solely on this class and don't need to do any … … 276 277 # 277 278 # Option examples: 278 279 # has_many :comments, :order => "posted_on" 280 # has_many :comments, :dependent => :nullify 279 281 # has_many :people, :class_name => "Person", :conditions => "deleted = 0", :order => "name" 280 # has_many :tracks, :order => "position", :dependent => true282 # has_many :tracks, :order => "position", :dependent => :destroy 281 283 # has_many :subscribers, :class_name => "Person", :finder_sql => 282 284 # 'SELECT DISTINCT people.* ' + 283 285 # 'FROM people p, post_subscriptions ps ' + … … 296 298 # See HasManyAssociation#delete_records. Dependent associations 297 299 # delete children, otherwise foreign key is set to NULL. 298 300 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 300 309 elsif options[:exclusively_dependent] 301 310 module_eval "before_destroy { |record| #{association_class_name}.delete_all(%(#{association_class_primary_key_name} = \#{record.quoted_id})) }" 302 311 end