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

Changeset 7511

Show
Ignore:
Timestamp:
09/18/07 10:26:56 (1 year ago)
Author:
nzkoz
Message:

Stop users from calling .create on a has_many / habtm association when the owner is a new_record?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/associations/association_collection.rb

    r7368 r7511  
    8686       
    8787      def create(attrs = {}) 
     88        ensure_owner_is_not_new 
    8889        record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.create(attrs) }                 
    8990        @target ||= [] unless loaded? 
     
    9394 
    9495      def create!(attrs = {}) 
     96        ensure_owner_is_not_new 
    9597        record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.create!(attrs) }                 
    9698        @target ||= [] unless loaded? 
     
    207209          full_callback_name = "#{callback_name}_for_#{@reflection.name}" 
    208210          @owner.class.read_inheritable_attribute(full_callback_name.to_sym) || [] 
    209         end         
     211        end    
     212         
     213        def ensure_owner_is_not_new 
     214          if @owner.new_record? 
     215            raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" 
     216          end 
     217        end 
     218                
    210219    end 
    211220  end 
  • trunk/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb

    r7368 r7511  
    1616      def create(attributes = {}) 
    1717        # Can't use Base.create because the foreign key may be a protected attribute. 
     18        ensure_owner_is_not_new 
    1819        if attributes.is_a?(Array) 
    1920          attributes.collect { |attr| create(attr) } 
     
    2324          record 
    2425        end 
     26      end 
     27       
     28      def create!(attributes = {}) 
     29        # Can't use Base.create! because the foreign key may be a protected attribute. 
     30        ensure_owner_is_not_new 
     31        if attributes.is_a?(Array) 
     32          attributes.collect { |attr| create(attr) } 
     33        else 
     34          record = build(attributes) 
     35          insert_record(record, true) unless @owner.new_record? 
     36          record 
     37        end         
    2538      end 
    2639 
     
    7689        end 
    7790 
    78         def insert_record(record
     91        def insert_record(record, force=true
    7992          if record.new_record? 
    80             return false unless record.save 
     93            if force 
     94              record.save! 
     95            else 
     96              return false unless record.save 
     97            end 
    8198          end 
    8299 
  • trunk/activerecord/test/associations_test.rb

    r7477 r7511  
    577577    assert_equal 3, first_firm.plain_clients.size 
    578578  end 
     579   
     580  def test_create_with_bang_on_has_many_when_parent_is_new_raises 
     581    assert_raises(ActiveRecord::RecordNotSaved) do  
     582      firm = Firm.new 
     583      firm.plain_clients.create! :name=>"Whoever" 
     584    end 
     585  end 
     586 
     587  def test_regular_create_on_has_many_when_parent_is_new_raises 
     588    assert_raises(ActiveRecord::RecordNotSaved) do  
     589      firm = Firm.new 
     590      firm.plain_clients.create :name=>"Whoever" 
     591    end 
     592  end 
     593   
     594  def test_create_with_bang_on_habtm_when_parent_is_new_raises 
     595    assert_raises(ActiveRecord::RecordNotSaved) do  
     596      Developer.new("name" => "Aredridel").projects.create!     
     597    end 
     598  end 
    579599 
    580600  def test_adding_a_mismatch_class 
     
    15411561  def test_create_by_new_record 
    15421562    devel = Developer.new(:name => "Marcel", :salary => 75000) 
    1543     proj1 = devel.projects.create(:name => "Make bed") 
    1544     proj2 = devel.projects.create(:name => "Lie in it") 
     1563    proj1 = devel.projects.build(:name => "Make bed") 
     1564    proj2 = devel.projects.build(:name => "Lie in it") 
    15451565    assert_equal devel.projects.last, proj2 
    15461566    assert proj2.new_record? 
  • trunk/activerecord/test/validations_test.rb

    r7407 r7511  
    676676    assert !t.save 
    677677    assert t.errors.on(:replies) 
    678     t.replies.create('title' => 'areply', 'content' => 'whateveragain') 
     678    reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') 
    679679    assert t.valid? 
    680680  end 
     
    881881      assert !t.save 
    882882      assert t.errors.on(:replies) 
    883       t.replies.create('title' => 'あいうえお', 'content' => 'かきくけこ') 
     883      t.replies.build('title' => 'あいうえお', 'content' => 'かきくけこ') 
    884884      assert t.valid? 
    885885    end