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

Changeset 8675

Show
Ignore:
Timestamp:
01/19/08 05:30:42 (8 months ago)
Author:
bitsweat
Message:

belongs_to supports :dependent => :destroy and :delete. Closes #10592.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r8672 r8675  
    11*SVN* 
     2 
     3* belongs_to supports :dependent => :destroy and :delete.  #10592 [Jonathan Viney] 
    24 
    35* Introduce preload query strategy for eager :includes.  #9640 [Frederick Cheung, Aleksey Kondratenko] 
  • trunk/activerecord/lib/active_record/associations.rb

    r8672 r8675  
    793793      #   of the association with an +_id+ suffix. So a class that defines a +belongs_to :person+ association will use +person_id+ as the default +foreign_key+. 
    794794      #   Similarly, +belongs_to :favorite_person, :class_name => "Person"+ will use a foreign key of +favorite_person_id+. 
     795      # * <tt>:dependent</tt>   - if set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to 
     796      #   <tt>:delete</tt>, the associated object is deleted *without* calling its destroy method. This option should not be specified when 
     797      #   <tt>belongs_to</tt> is used in conjunction with a <tt>has_many</tt> relationship on another class because of the potential to leave 
     798      #   orphaned records behind. 
    795799      # * <tt>:counter_cache</tt> - caches the number of belonging objects on the associate class through the use of +increment_counter+ 
    796800      #   and +decrement_counter+. The counter cache is incremented when an object of this class is created and decremented when it's 
     
    877881          ) 
    878882        end 
     883 
     884        configure_dependency_for_belongs_to(reflection) 
    879885      end 
    880886 
     
    12031209        end 
    12041210 
     1211        def configure_dependency_for_belongs_to(reflection) 
     1212          if reflection.options.include?(:dependent) 
     1213            case reflection.options[:dependent] 
     1214              when :destroy 
     1215                module_eval "before_destroy '#{reflection.name}.destroy unless #{reflection.name}.nil?'" 
     1216              when :delete 
     1217                module_eval "before_destroy '#{reflection.class_name}.delete(#{reflection.name}.id) unless #{reflection.name}.nil?'" 
     1218              else 
     1219                raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})" 
     1220            end 
     1221          end 
     1222        end 
     1223 
    12051224        def create_has_many_reflection(association_id, options, &extension) 
    12061225          options.assert_valid_keys( 
  • trunk/activerecord/test/cases/associations_test.rb

    r8672 r8675  
    471471class HasManyAssociationsTest < ActiveSupport::TestCase 
    472472  fixtures :accounts, :companies, :developers, :projects, 
    473            :developers_projects, :topics, :authors, :comments 
     473           :developers_projects, :topics, :authors, :comments, :author_addresses 
    474474 
    475475  def setup 
     
    994994    # only the correctly associated client should have been deleted 
    995995    assert_equal 1, Client.find_all_by_client_of(firm.id).size 
     996  end 
     997 
     998  def test_dependent_delete_and_destroy_with_belongs_to 
     999    author_address = author_addresses(:david_address) 
     1000    assert_equal [], AuthorAddress.destroyed_author_address_ids[authors(:david).id] 
     1001 
     1002    assert_difference "AuthorAddress.count", -2 do 
     1003      authors(:david).destroy 
     1004    end 
     1005 
     1006    assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids[authors(:david).id] 
     1007  end 
     1008 
     1009  def test_invalid_belongs_to_dependent_option_raises_exception 
     1010    assert_raises ArgumentError do 
     1011      Author.belongs_to :special_author_address, :dependent => :nullify 
     1012    end 
    9961013  end 
    9971014 
  • trunk/activerecord/test/cases/json_serialization_test.rb

    r8661 r8675  
    152152    authors = [@david, @mary] 
    153153 
    154     assert_equal %([{"id": 1}, {"id": 2}]), authors.to_json(:except => [:name, :author_address_id]) 
     154    assert_equal %([{"id": 1}, {"id": 2}]), authors.to_json(:except => [:name, :author_address_id, :author_address_extra_id]) 
    155155  end 
    156156 
  • trunk/activerecord/test/fixtures/authors.yml

    r1083 r8675  
    22  id: 1 
    33  name: David 
     4  author_address_id: 1 
     5  author_address_extra_id: 2 
    46 
    57mary: 
  • trunk/activerecord/test/models/author.rb

    r8657 r8675  
    6666  has_many :post_categories, :through => :posts, :source => :categories 
    6767 
    68   belongs_to :author_address 
     68  belongs_to :author_address, :dependent => :destroy 
     69  belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" 
    6970 
    7071  attr_accessor :post_log 
     
    102103class AuthorAddress < ActiveRecord::Base 
    103104  has_one :author 
     105 
     106  def self.destroyed_author_address_ids 
     107    @destroyed_author_address_ids ||= Hash.new { |h,k| h[k] = [] } 
     108  end 
     109 
     110  before_destroy do |author_address| 
     111    if author_address.author 
     112      AuthorAddress.destroyed_author_address_ids[author_address.author.id] << author_address.id 
     113    end 
     114    true 
     115  end 
    104116end 
    105117 
  • trunk/activerecord/test/schema/schema.rb

    r8659 r8675  
    240240  add_column :posts, :taggings_count, :integer, :default => 0 
    241241  add_column :authors, :author_address_id, :integer 
     242  add_column :authors, :author_address_extra_id, :integer 
    242243 
    243244  create_table :author_addresses, :force => true do |t| 
    244     t.column :author_address_id, :integer 
    245245  end 
    246246