# Migration : value between 1 and 7

**URL:** <https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352>\
**Category:** Rails\
**Created:** [February 25, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352 "2009-02-25T19:49:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/1 "2009-02-25T19:49:41Z")

</div>

Hello everyone!

I want to add a column to a table. So here is my migration. My question  
is : Is it possible to say that this value must be between 1 and 7 and  
must no be a float (1.4 shouldn’t be possible) ?

class AddValueToTable \< ActiveRecord::Migration  
def self.up  
add\_column :table, :value, :int  
end

def self.down  
remove\_column :table, :value  
end  
end

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/2 "2009-02-25T19:54:07Z")

</div>

> def self.down  
> remove\_column :table, :value  
> end  
> end

Well, if you define the column as an integer you’ll never end up with  
1.4 as a value. It will always get truncated to an integer.  
Depending on your database you can add those types of restrictions. I  
don’t think rails migrations has support for it directly though.

You could also do it as a before\_save validation in the model. You’ll  
probably want to do this anyway so that if a user enters “1.4” you can  
complain early.

-philip

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 7:55pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/3 "2009-02-25T19:55:19Z")

</div>

This isn’t someithing to be done inside a migration, but at your model  
as a validation:

class Table  
validates\_inclusion\_of :value, :in =\> (1…7).to\_i  
end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Wed, Feb 25, 2009 at 3:49 PM, Guillaume L.

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/4 "2009-02-25T19:56:02Z")

</div>

I’m using mySQL. Can I add a parameter :min and :max?

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/5 "2009-02-25T19:56:32Z")

</div>

Ops, it’s a to\_a

class Table  
validates\_inclusion\_of :value, :in =\> (1…7).to\_a  
end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Wed, Feb 25, 2009 at 3:54 PM, Maurício Linhares

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/6 "2009-02-25T19:57:03Z")

</div>

Ok so I’'ll dot it at my model. Thank you!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/7 "2009-02-25T20:06:37Z")

</div>

'Cos:

(1…7).include?( 2.2 ) is true, but (1…7).to\_a.include?( 2.2 ) isn’t.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Wed, Feb 25, 2009 at 4:02 PM, Guillaume L.

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/8 "2009-02-25T20:02:05Z")

</div>

MaurÃ­cio Linhares wrote:

> Ops, it’s a to\_a

Why do we need to\_a?

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 25, 2009, 8:06pm UTC](https://www.ruby-forum.com/t/migration-value-between-1-and-7/161352/9 "2009-02-25T20:06:38Z")

</div>

Add to model

validates\_numericality\_of :value,  
:only\_integer =\> true,  
:less\_than\_or\_equal\_to =\> 7,  
:greater\_than\_or\_equal\_to =\> 1

to get the inclusive [1-7] int only

On Feb 25, 8:49 am, Guillaume L. \<rails-mailing-l…@andreas-
