# Validates\_associated trouble

**URL:** <https://www.ruby-forum.com/t/validates-associated-trouble/86284>\
**Category:** Rails\
**Created:** [February 21, 2007, 4:26am UTC](https://www.ruby-forum.com/t/validates-associated-trouble/86284 "2007-02-21T04:26:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [February 21, 2007, 4:26am UTC](https://www.ruby-forum.com/t/validates-associated-trouble/86284/1 "2007-02-21T04:26:33Z")

</div>

hello friends,

I need to modelate a genealogical tree with:  
animal,Dam(Mother) and Sire(Father)

the Dam and Sire are Animals then i modelate this:

class Animal \< ActiveRecord::Base  
belongs\_to :sire  
belongs\_to :dam  
validates\_associated :sire, :dam  
end

class Sire \< Animal  
has\_many :animals  
end

class Dam \< Animal  
has\_many :animals  
end

then when i save a new animal the validate doesnt work like this:

i have the table Animals

ID, Name Father MOther  
1 A - -  
2 B - -

when I insert a new record like this

insert(3,C,1,2) its ok!  
insert(3,C,5,7) its ok too, (the validates is doesnt work)

how can i solve this??

---

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [February 21, 2007, 5:45am UTC](https://www.ruby-forum.com/t/validates-associated-trouble/86284/2 "2007-02-21T05:45:47Z")

</div>

Alfredo G. wrote:

> validates\_associated :sire, :dam  
> then when i save a new animal the validate doesnt work like this:  
> insert(3,C,5,7) its ok too, (the validates is doesnt work)
> 
> how can i solve this??

Use validates\_presence\_of instead of validates\_associated.

Or use in addition to validates\_associated if you want to  
check some attributes of the associates.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [February 21, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/validates-associated-trouble/86284/3 "2007-02-21T14:11:46Z")

</div>

Mark Reginald J. wrote:

> Alfredo G. wrote:
> 
> > validates\_associated :sire, :dam  
> > then when i save a new animal the validate doesnt work like this:  
> > insert(3,C,5,7) its ok too, (the validates is doesnt work)
> > 
> > how can i solve this??
> 
> Use validates\_presence\_of instead of validates\_associated.
> 
> Or use in addition to validates\_associated if you want to  
> check some attributes of the associates.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

Thanks friend.

the problem is that in some cases an Animals has not father or mother,  
then how can i do it??

---

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [February 21, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/validates-associated-trouble/86284/4 "2007-02-21T14:55:40Z")

</div>

Alfredo G. wrote:

> > Use validates\_presence\_of instead of validates\_associated.
> 
> the problem is that in some cases an Animals has not father or mother,  
> then how can i do it??

This should allow clones if your foreign\_keys are default NULL:

validates\_presence\_of :sire, :if =\> Proc { |animal| animal.sire\_id }  
validates\_presence\_of :dam, :if =\> Proc { |animal| animal.dam\_id }

If instead they’re default 0, the condition becomes

```
animal.sire_id > 0

```

etc.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [February 22, 2007, 12:26am UTC](https://www.ruby-forum.com/t/validates-associated-trouble/86284/5 "2007-02-22T00:26:49Z")

</div>

Alfredo G. wrote:

> > validates\_presence\_of :sire, :if =\> Proc { |animal| animal.sire\_id }
> 
> what is Proc?? i have to write the Proc method??

A Proc is a mini function that doesn’t have a name. You can pass them  
around as parameters to methods. See:

```
http://www.ruby-doc.org/core/classes/Proc.html

```

Note that I incorrectly capitalized “proc”. You instead  
have to write either

proc { |animal| animal.sire\_id }

or

Proc.new { |animal| animal.sire\_id }

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![alfredogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@alfredogz](https://www.ruby-forum.com/u/alfredogz)\
**Post date:** [February 21, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/validates-associated-trouble/86284/6 "2007-02-21T17:04:20Z")

</div>

Mark Reginald J. wrote:

> Alfredo G. wrote:
> 
> > > Use validates\_presence\_of instead of validates\_associated.
> > 
> > the problem is that in some cases an Animals has not father or mother,  
> > then how can i do it??
> 
> This should allow clones if your foreign\_keys are default NULL:
> 
> validates\_presence\_of :sire, :if =\> Proc { |animal| animal.sire\_id }  
> validates\_presence\_of :dam, :if =\> Proc { |animal| animal.dam\_id }
> 
> If instead they’re default 0, the condition becomes
> 
> ```
> animal.sire_id > 0
> 
> ```
> 
> etc.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

THanks a lot for your help,

what is Proc?? i have to write the Proc method??

i newbie in RoR sorry
