# Validate creation of both parent and child from same form?

**URL:** <https://www.ruby-forum.com/t/validate-creation-of-both-parent-and-child-from-same-form/149610>\
**Category:** Rails\
**Created:** [September 26, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/validate-creation-of-both-parent-and-child-from-same-form/149610 "2008-09-26T18:46:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [September 26, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/validate-creation-of-both-parent-and-child-from-same-form/149610/1 "2008-09-26T18:46:47Z")

</div>

Alternate subject title: Validate child of unsaved parent based on  
grandparent attr?

I’ve got a create view with a form for both the parent object as well as  
multiple child objects. Upon submit, can I validate the parent and child  
objects before either are saved? There are a few caveats:

1.) The validation of one attribute on all the child objects is based on  
an attribute of the parent’s parent! (The grandparent object is already  
saved)

2.) The parent object may have an attachment that must also be valid.  
(using attachment\_fu)

class Project  
has\_one :settlement  
end

class Settlement  
belongs\_to :project  
has\_many :details,  
:class\_name =\> ‘SettlementDetail’,  
:dependent =\> :delete\_all do  
def total\_quantity  
inject(0){|sum, p| sum + (p.quantity.nil? ? 0 : p.quantity)}  
end

```
  def total_weight
    inject(0){|sum, p| sum + (p.weight.nil? ? 0 : p.weight)}
  end
end

```

end

class SettlementDetail \< ActiveRecord::Base  
belongs\_to :settlement

validates\_presence\_of :quantity, :if =\> :quantity\_required

def quantity\_required  
### TODO Will this work on a new detail object with an unsaved  
### parent settlement object?

```
if self.settlement.project.billing_method == "Weight"
  # We don't need to validate because the billing method is Weight
  return false
else
  # Count is required
  return true
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [September 27, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/validate-creation-of-both-parent-and-child-from-same-form/149610/2 "2008-09-27T12:56:01Z")

</div>

Chris B. wrote:

> ```
> :class_name => 'SettlementDetail',
> 
> ```
> 
> class SettlementDetail \< ActiveRecord::Base  
> belongs\_to :settlement
> 
> validates\_presence\_of :quantity, :if =\> :quantity\_required
> 
> def quantity\_required  
> ### TODO Will this work on a new detail object with an unsaved  
> ### parent settlement object?

Yes, as long as you set the settlement association of each  
settlement\_detail child you build to the new settlement object,  
and also set the project association of that settlement object.

> ```
> if self.settlement.project.billing_method == "Weight"
> # We don't need to validate because the billing method is Weight
> return false
> else
> # Count is required
> return true
> end
> 
> ```
> 
> end  
> end

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [September 30, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/validate-creation-of-both-parent-and-child-from-same-form/149610/3 "2008-09-30T19:47:39Z")

</div>

Mark J. wrote:

> Yes, as long as you set the settlement association of each  
> settlement\_detail child you build to the new settlement object,  
> and also set the project association of that settlement object.

Thanks for confirming. I’m making some changes to this particular  
process so I may have more questions.
