Changeset 7823
- Timestamp:
- 10/10/07 05:42:52 (1 year ago)
- Files:
-
- branches/1-2-stable/activerecord/lib/active_record/associations.rb (modified) (1 diff)
- branches/1-2-stable/activerecord/lib/active_record/associations/association_collection.rb (modified) (1 diff)
- branches/1-2-stable/activerecord/test/associations_test.rb (modified) (2 diffs)
- branches/1-2-stable/activerecord/test/fixtures/db_definitions/schema.rb (modified) (1 diff)
- branches/1-2-stable/activerecord/test/fixtures/developer.rb (modified) (1 diff)
- branches/1-2-stable/activerecord/test/validations_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1-2-stable/activerecord/lib/active_record/associations.rb
r7076 r7823 995 995 association = instance_variable_get("@#{association_name}") 996 996 997 if association.respond_to?(:loaded?) && association.loaded? 998 if @new_record_before_save 999 records_to_save = association 1000 else 1001 records_to_save = association.select { |record| record.new_record? } 1002 end 997 records_to_save = if @new_record_before_save 998 association 999 elsif association.respond_to?(:loaded?) && association.loaded? 1000 association.select { |record| record.new_record? } 1001 else 1002 [] 1003 end 1004 1005 if !records_to_save.blank? 1003 1006 records_to_save.each { |record| association.send(:insert_record, record) } 1004 1007 association.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id branches/1-2-stable/activerecord/lib/active_record/associations/association_collection.rb
r5018 r7823 92 92 else 93 93 record = build(attributes) 94 record.save unless @owner.new_record? 94 if @owner.new_record? 95 ActiveSupport::Deprecation.warn("Calling .create on a has_many association without saving its owner will not work in rails 2.0, you probably want .build instead") 96 else 97 record.save 98 end 95 99 record 96 100 end branches/1-2-stable/activerecord/test/associations_test.rb
r7076 r7823 96 96 assert !david.projects.loaded? 97 97 end 98 99 def test_save_on_parent_saves_children 100 developer = Developer.create :name => "Bryan", :salary => 50_000 101 assert_equal 1, developer.reload.audit_logs.size 102 end 98 103 end 99 104 … … 592 597 end 593 598 599 def test_regular_create_on_has_many_when_parent_is_new_raises 600 assert_deprecated(/.build instead/) do 601 firm = Firm.new 602 firm.plain_clients.create :name=>"Whoever" 603 end 604 end 605 594 606 def test_adding_a_mismatch_class 595 607 assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil } branches/1-2-stable/activerecord/test/fixtures/db_definitions/schema.rb
r5385 r7823 58 58 t.column :custom_lock_version, :integer 59 59 end 60 61 create_table :audit_logs, :force => true do |t| 62 t.column :message, :string, :null=>false 63 t.column :developer_id, :integer, :null=>false 64 end 60 65 end branches/1-2-stable/activerecord/test/fixtures/developer.rb
r4206 r7823 32 32 has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id' 33 33 34 has_many :audit_logs 35 34 36 validates_inclusion_of :salary, :in => 50000..200000 35 37 validates_length_of :name, :within => 3..20 38 39 before_create do |developer| 40 developer.audit_logs.build :message => "Computer created" 41 end 42 end 43 44 class AuditLog < ActiveRecord::Base 45 belongs_to :developer 36 46 end 37 47 branches/1-2-stable/activerecord/test/validations_test.rb
r5590 r7823 632 632 assert !t.save 633 633 assert t.errors.on(:replies) 634 t.replies. create('title' => 'areply', 'content' => 'whateveragain')634 t.replies.build('title' => 'areply', 'content' => 'whateveragain') 635 635 assert t.valid? 636 636 end … … 837 837 assert !t.save 838 838 assert t.errors.on(:replies) 839 t.replies. create('title' => 'ããããã', 'content' => 'ããããã')839 t.replies.build('title' => 'ããããã', 'content' => 'ããããã') 840 840 assert t.valid? 841 841 end