Skip to content
This repository
Browse code

Note about validates_presence_of and nested attributes

  • Loading branch information...
commit 99c092d4e875856502946c29a1ae484eab3befb8 1 parent e6b6d3a
Trevor Turk authored August 19, 2010
16  activerecord/lib/active_record/nested_attributes.rb
@@ -202,6 +202,22 @@ class TooManyRecords < ActiveRecordError
202 202
     # You would need to modify it to look like this:
203 203
     #
204 204
     #   attr_accessible :name, :posts_attributes
  205
+    #
  206
+    # === Validating the presence of a parent model
  207
+    #
  208
+    # If you want to validate that a child record is associated with a parent
  209
+    # record, you can use <tt>validates_presence_of</tt> and
  210
+    # <tt>inverse_of</tt> as this example illustrates:
  211
+    #
  212
+    #   class Member < ActiveRecord::Base
  213
+    #     has_many :posts, :inverse_of => :member
  214
+    #     accepts_nested_attributes_for :posts
  215
+    #   end
  216
+    #
  217
+    #   class Post < ActiveRecord::Base
  218
+    #     belongs_to :member, :inverse_of => :posts
  219
+    #     validates_presence_of :member
  220
+    #   end
205 221
     module ClassMethods
206 222
       REJECT_ALL_BLANK_PROC = proc { |attributes| attributes.all? { |_, value| value.blank? } }
207 223
 

0 notes on commit 99c092d

Please sign in to comment.
Something went wrong with that request. Please try again.