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

Changeset 7824

Show
Ignore:
Timestamp:
10/10/07 06:45:13 (1 year ago)
Author:
nzkoz
Message:

Ensure that 'autosaving' works when associations aren't loaded [Bryan Helmkamp] References #8713

Files:

Legend:

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

    r7813 r7824  
    10731073            association = instance_variable_get("@#{association_name}") 
    10741074 
    1075             if association.respond_to?(:loaded?) && association.loaded? 
    1076               if @new_record_before_save 
    1077                 records_to_save = association 
    1078               else 
    1079                 records_to_save = association.select { |record| record.new_record? } 
    1080               end 
     1075            records_to_save = if @new_record_before_save 
     1076              association 
     1077            elsif association.respond_to?(:loaded?) && association.loaded? 
     1078              association.select { |record| record.new_record? } 
     1079            else 
     1080              [] 
     1081            end 
     1082 
     1083            if !records_to_save.blank? 
    10811084              records_to_save.each { |record| association.send(:insert_record, record) } 
    10821085              association.send(:construct_sql)   # reconstruct the SQL queries now that we know the owner's id 
  • trunk/activerecord/test/associations_test.rb

    r7767 r7824  
    102102  end 
    103103 
     104  def test_save_on_parent_saves_children 
     105    developer = Developer.create :name => "Bryan", :salary => 50_000 
     106    assert_equal 1, developer.reload.audit_logs.size 
     107  end 
    104108end 
    105109 
  • trunk/activerecord/test/fixtures/db_definitions/schema.rb

    r7119 r7824  
    7676    end 
    7777  end 
     78 
     79  create_table :audit_logs, :force => true do |t| 
     80    t.column :message, :string, :null=>false 
     81    t.column :developer_id, :integer, :null=>false 
     82  end 
    7883end 
  • trunk/activerecord/test/fixtures/developer.rb

    r7504 r7824  
    4242  has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id' 
    4343 
     44  has_many :audit_logs 
     45 
    4446  validates_inclusion_of :salary, :in => 50000..200000 
    4547  validates_length_of    :name, :within => 3..20 
     48 
     49  before_create do |developer| 
     50    developer.audit_logs.build :message => "Computer created" 
     51  end 
     52end 
     53 
     54class AuditLog < ActiveRecord::Base 
     55  belongs_to :developer 
    4656end 
    4757