Skip to content

Commit

Permalink
Define autosave association callbacks when using accepts_nested_attri…
Browse files Browse the repository at this point in the history
…butes_for.

This way we don't define all the validation methods for all associations by
default, but only when needed.

[#3355 state:resolved]
  • Loading branch information
alloy committed Nov 6, 2009
1 parent c2cfb20 commit f125a34
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/nested_attributes.rb
Expand Up @@ -250,6 +250,8 @@ def #{association_name}_attributes=(attributes)
assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
end
}, __FILE__, __LINE__

add_autosave_association_callbacks(reflection)
else
raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/models/pirate.rb
Expand Up @@ -18,7 +18,7 @@ class Pirate < ActiveRecord::Base
has_many :treasure_estimates, :through => :treasures, :source => :price_estimates

# These both have :autosave enabled because accepts_nested_attributes_for is used on them.
has_one :ship, :validate => true
has_one :ship
has_one :non_validated_ship, :class_name => 'Ship'
has_many :birds
has_many :birds_with_method_callbacks, :class_name => "Bird",
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/models/ship.rb
@@ -1,7 +1,7 @@
class Ship < ActiveRecord::Base
self.record_timestamps = false

belongs_to :pirate, :validate => true
belongs_to :pirate
has_many :parts, :class_name => 'ShipPart', :autosave => true

accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
Expand Down

0 comments on commit f125a34

Please sign in to comment.