Changeset 8675
- Timestamp:
- 01/19/08 05:30:42 (8 months ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations.rb (modified) (3 diffs)
- trunk/activerecord/test/cases/associations_test.rb (modified) (2 diffs)
- trunk/activerecord/test/cases/json_serialization_test.rb (modified) (1 diff)
- trunk/activerecord/test/fixtures/authors.yml (modified) (1 diff)
- trunk/activerecord/test/models/author.rb (modified) (2 diffs)
- trunk/activerecord/test/schema/schema.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r8672 r8675 1 1 *SVN* 2 3 * belongs_to supports :dependent => :destroy and :delete. #10592 [Jonathan Viney] 2 4 3 5 * Introduce preload query strategy for eager :includes. #9640 [Frederick Cheung, Aleksey Kondratenko] trunk/activerecord/lib/active_record/associations.rb
r8672 r8675 793 793 # 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+. 794 794 # 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. 795 799 # * <tt>:counter_cache</tt> - caches the number of belonging objects on the associate class through the use of +increment_counter+ 796 800 # and +decrement_counter+. The counter cache is incremented when an object of this class is created and decremented when it's … … 877 881 ) 878 882 end 883 884 configure_dependency_for_belongs_to(reflection) 879 885 end 880 886 … … 1203 1209 end 1204 1210 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 1205 1224 def create_has_many_reflection(association_id, options, &extension) 1206 1225 options.assert_valid_keys( trunk/activerecord/test/cases/associations_test.rb
r8672 r8675 471 471 class HasManyAssociationsTest < ActiveSupport::TestCase 472 472 fixtures :accounts, :companies, :developers, :projects, 473 :developers_projects, :topics, :authors, :comments 473 :developers_projects, :topics, :authors, :comments, :author_addresses 474 474 475 475 def setup … … 994 994 # only the correctly associated client should have been deleted 995 995 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 996 1013 end 997 1014 trunk/activerecord/test/cases/json_serialization_test.rb
r8661 r8675 152 152 authors = [@david, @mary] 153 153 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]) 155 155 end 156 156 trunk/activerecord/test/fixtures/authors.yml
r1083 r8675 2 2 id: 1 3 3 name: David 4 author_address_id: 1 5 author_address_extra_id: 2 4 6 5 7 mary: trunk/activerecord/test/models/author.rb
r8657 r8675 66 66 has_many :post_categories, :through => :posts, :source => :categories 67 67 68 belongs_to :author_address 68 belongs_to :author_address, :dependent => :destroy 69 belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" 69 70 70 71 attr_accessor :post_log … … 102 103 class AuthorAddress < ActiveRecord::Base 103 104 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 104 116 end 105 117 trunk/activerecord/test/schema/schema.rb
r8659 r8675 240 240 add_column :posts, :taggings_count, :integer, :default => 0 241 241 add_column :authors, :author_address_id, :integer 242 add_column :authors, :author_address_extra_id, :integer 242 243 243 244 create_table :author_addresses, :force => true do |t| 244 t.column :author_address_id, :integer245 245 end 246 246