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

Changeset 7823

Show
Ignore:
Timestamp:
10/10/07 05:42:52 (1 year ago)
Author:
nzkoz
Message:

Add deprecation warning for calling .create on has_many associations with an unsaved owner.

Fix regression introduced by [7076] by restoring 1.2.3 behaviour for saving associated records [Bryan Helmkamp]. References #8713

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-stable/activerecord/lib/active_record/associations.rb

    r7076 r7823  
    995995            association = instance_variable_get("@#{association_name}") 
    996996 
    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? 
    10031006              records_to_save.each { |record| association.send(:insert_record, record) } 
    10041007              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  
    9292        else 
    9393          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 
    9599          record 
    96100        end 
  • branches/1-2-stable/activerecord/test/associations_test.rb

    r7076 r7823  
    9696    assert !david.projects.loaded? 
    9797  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 
    98103end 
    99104 
     
    592597  end 
    593598   
     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 
    594606  def test_adding_a_mismatch_class 
    595607    assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil } 
  • branches/1-2-stable/activerecord/test/fixtures/db_definitions/schema.rb

    r5385 r7823  
    5858    t.column :custom_lock_version, :integer 
    5959  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 
    6065end 
  • branches/1-2-stable/activerecord/test/fixtures/developer.rb

    r4206 r7823  
    3232  has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id' 
    3333 
     34  has_many :audit_logs 
     35 
    3436  validates_inclusion_of :salary, :in => 50000..200000 
    3537  validates_length_of    :name, :within => 3..20 
     38 
     39  before_create do |developer| 
     40    developer.audit_logs.build :message => "Computer created" 
     41  end 
     42end 
     43 
     44class AuditLog < ActiveRecord::Base 
     45  belongs_to :developer 
    3646end 
    3747 
  • branches/1-2-stable/activerecord/test/validations_test.rb

    r5590 r7823  
    632632    assert !t.save 
    633633    assert t.errors.on(:replies) 
    634     t.replies.create('title' => 'areply', 'content' => 'whateveragain') 
     634    t.replies.build('title' => 'areply', 'content' => 'whateveragain') 
    635635    assert t.valid? 
    636636  end 
     
    837837      assert !t.save 
    838838      assert t.errors.on(:replies) 
    839       t.replies.create('title' => 'あいうえお', 'content' => 'かきくけこ') 
     839      t.replies.build('title' => 'あいうえお', 'content' => 'かきくけこ') 
    840840      assert t.valid? 
    841841    end