Permalink
Browse files

Define autosave association callbacks when using accepts_nested_attri…

…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...
1 parent c3ef028 commit 6b2291f33063b6742cba84f5f64e03de9907c1f8 @alloy alloy committed Nov 6, 2009
@@ -239,6 +239,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
@@ -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",
@@ -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? }

0 comments on commit 6b2291f

Please sign in to comment.