# Validations in the same Model

**URL:** <https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670>\
**Category:** rubyonrails-talk\
**Created:** [April 26, 2011, 4:35pm UTC](https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670 "2011-04-26T16:35:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junior\_Grossi](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Junior\_Grossi](https://discuss.rubyonrails.org/u/Junior_Grossi)\
**Post date:** [April 26, 2011, 4:35pm UTC](https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670/1 "2011-04-26T16:35:56Z")

</div>

Hi all,

I am newbie in Rails and new at this group. I am with a problem with validation. I searched at Google but I cant see anything.

I have just one Model: User. I have 2 CRUD's (controllers and views): Representatives and Users... Users CRUD is ok but Representatives not. I have different validations rules for Representatives and Users, but I have to use the same table in the DB, so the same Model.

How can I make different validations in a same Model?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://discuss.rubyonrails.org/u/Philip_Hallstrom)\
**Post date:** [April 26, 2011, 4:58pm UTC](https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670/2 "2011-04-26T16:58:31Z")

</div>

Look into STI (single table inheritance). This will let you use the same underlying table (and share common functionality) among multiple models...

-philip

---

<div class="post-metadata">

**Author:** ![Junior\_Grossi](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Junior\_Grossi](https://discuss.rubyonrails.org/u/Junior_Grossi)\
**Post date:** [April 26, 2011, 6:29pm UTC](https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670/3 "2011-04-26T18:29:27Z")

</div>

Hi philip!!!

Thanks for your reply. I read about STI and solve my problem.

BUT now I have another hehe. I have 2 validations rules, because I have a has\_one association. How can I do this?

[http://pastie.org/1836266](http://pastie.org/1836266)

For example... I have to validate :name (Profile) for Representative, but not for User.

Thanks!

---

<div class="post-metadata">

**Author:** ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://discuss.rubyonrails.org/u/Philip_Hallstrom)\
**Post date:** [April 26, 2011, 10:43pm UTC](https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670/4 "2011-04-26T22:43:51Z")

</div>

> Thanks for your reply. I read about STI and solve my problem.
> 
> BUT now I have another hehe. I have 2 validations rules, because I have a has\_one association. How can I do this?
> 
> [http://pastie.org/1836266](http://pastie.org/1836266)
> 
> For example... I have to validate :name (Profile) for Representative, but not for User.

class Profile...

validates :name, :presence =\> {:if =\> lambda {|p| p.representive.present?}}

end

Or something like that... might need to check the class type instead... that should get you started though.

---

<div class="post-metadata">

**Author:** ![Junior\_Grossi](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Junior\_Grossi](https://discuss.rubyonrails.org/u/Junior_Grossi)\
**Post date:** [April 27, 2011, 12:41pm UTC](https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670/5 "2011-04-27T12:41:53Z")

</div>

Thanks! I will try do something like that.

s

---

<div class="post-metadata">

**Author:** ![Junior\_Grossi](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Junior\_Grossi](https://discuss.rubyonrails.org/u/Junior_Grossi)\
**Post date:** [April 27, 2011, 4:20pm UTC](https://discuss.rubyonrails.org/t/validations-in-the-same-model/58670/6 "2011-04-27T16:20:28Z")

</div>

Just to complete the topic... solved!

Solution:

validates\_presence\_of :name, :tel\_commercial, :tel\_mobile, :address, :city, :state, :neighborhood, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:if =\> :representative?

&nbsp;&nbsp;def representative? &nbsp;&nbsp;&nbsp;&nbsp;lambda {|p| p.representative.present?} &nbsp;&nbsp;end

Thanks philip! s
