# Rails - DB Question

**URL:** <https://www.ruby-forum.com/t/rails-db-question/130828>\
**Category:** Rails\
**Created:** [March 3, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/rails-db-question/130828 "2008-03-03T18:27:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 3, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/rails-db-question/130828/1 "2008-03-03T18:27:25Z")

</div>

Hi-

This is more of a DB question…but say I have a model that has a  
field that I want to populate with specific items, see below for an  
example. What is the best way to do this in Rails?

Model:  
name, type, date - where “type” must be one of “male”, “female”.

Do I do this in the DB through constraints, or in the Rails app?

Thanks for the feedback!

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 3, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/rails-db-question/130828/2 "2008-03-03T19:03:20Z")

</div>

You can to the male/female checking in your model. Add this at the top:  
validates\_inclusion\_of :type, :in =\> %w( male female ), :message =\>  
“woah!  
what are you then!??!!”

On Mon, Mar 3, 2008 at 9:26 AM, pete [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Do I do this in the DB through constraints, or in the Rails app?
> 
> Thanks for the feedback!
> 
> >

–  
Henry  
[http://www.henrywagner.org/](http://www.henrywagner.org/)

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 3, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/rails-db-question/130828/3 "2008-03-03T19:17:33Z")

</div>

> You can to the male/female checking in your model. Add this at the top:
> 
> validates\_inclusion\_of :type, :in =\> %w( male female ), :message =\> “woah!  
> what are you then!??!!”

Be wary the word “type” unless you’re using STI or something else  
Rails specific.

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 3, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/rails-db-question/130828/4 "2008-03-03T20:14:02Z")

</div>

Haha, OK, I like that.

But what if someone connects to the DB from outside of the Rails app  
and populates the DB from another source? There is no constraint on  
the table, right?

PS I am not using “type”, just an example.

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [March 3, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/rails-db-question/130828/5 "2008-03-03T22:17:38Z")

</div>

Correct, the db would not have the constraint on it. In that case you  
probably want a constraint at both the db and model level. The nice  
thing  
about using the model level constraint is it uses Rails validation and  
will  
give a more user friendly error message than having to deal with a db  
level  
constraint violation, and present that error to the user.

On Mon, Mar 3, 2008 at 11:13 AM, pete [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > You can to the male/female checking in your model. Add this at the  
> > > top:
> > 
> > > validates\_inclusion\_of :type, :in =\> %w( male female ), :message =\>  
> > > “woah!  
> > > what are you then!??!!”
> > 
> > Be wary the word “type” unless you’re using STI or something else  
> > Rails specific.

–  
Henry  
[http://www.henrywagner.org/](http://www.henrywagner.org/)
