# Validation dependent on unsaved parent

**URL:** <https://www.ruby-forum.com/t/validation-dependent-on-unsaved-parent/135615>\
**Category:** Rails\
**Created:** [April 23, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/validation-dependent-on-unsaved-parent/135615 "2008-04-23T16:55:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IndyCodeWarrior](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@IndyCodeWarrior](https://www.ruby-forum.com/u/IndyCodeWarrior)\
**Post date:** [April 23, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/validation-dependent-on-unsaved-parent/135615/1 "2008-04-23T16:55:44Z")

</div>

I’m having trouble with a validation that depends on an attribute of a  
belongs\_to parent. If the child is added to an unsaved parent  
(parent.children \<\< new child), the has\_many collection  
parent.children includes the unsaved child. However the belongs\_to  
attribute child.parent appears to be nil until the parent has been  
saved. Without access to the parent attributes, the validation can’t  
be done in the child model. I have a sort-of workaround using  
validation code in the parent, but that violates DRY. The validation  
still has to be in the child because the child can be updated without  
the parent validation coming into play.

Here’s the code:

=============

class Item \< ActiveRecord::Base  
has\_many :parlevels  
has\_many :locations, :through =\> :parlevels  
has\_many :line\_items  
has\_many :orders, :through =\> :line\_items  
end

class Location \< ActiveRecord::Base  
has\_many :orders  
has\_many :parlevels  
has\_many :items, :through =\> :parlevels  
end

class Parlevel \< ActiveRecord::Base  
belongs\_to :item  
belongs\_to :location

#Attribute quantity is the max quantity of an item which may be  
ordered for a location  
end

class Order \< ActiveRecord::Base  
has\_many :line\_items, :dependent =\> :destroy  
belongs\_to :location  
end

class LineItem \< ActiveRecord::Base  
belongs\_to :order  
belongs\_to :item

def parlevel  
if self.order  
Parlevel.find(:first, :conditions =\> {:location\_id =\>  
self.order.location\_id, :item\_id =\> self.item\_id})  
else  
nil  
end  
end

def validate  
if self.quantity\_requested

```
  if self.parlevel
    limit = self.parlevel.quantity
  else
    limit = 0
  end

  errors.add(:quantity_requested,"above maximum of " + limit.to_s)

```

if self.quantity\_requested \> limit  
end  
end  
end

=============

The current result of this code in the limit is always zero when the  
parent is unsaved. Not very useful.

Any suggestions?

-Mack

---

<div class="post-metadata">

**Author:** ![IndyCodeWarrior](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@IndyCodeWarrior](https://www.ruby-forum.com/u/IndyCodeWarrior)\
**Post date:** [April 26, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/validation-dependent-on-unsaved-parent/135615/2 "2008-04-26T22:16:34Z")

</div>

Here’s the solution:

[http://macksmind.net/2008/04/26/using-belongs\_to-in-rails-model-validations-when-the-parent-is-unsaved/](http://macksmind.net/2008/04/26/using-belongs_to-in-rails-model-validations-when-the-parent-is-unsaved/)

On Wed, Apr 23, 2008 at 10:54 AM, IndyCodeWarrior
