# How to check if values of tables are within range?

**URL:** <https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709>\
**Category:** Rails\
**Created:** [April 24, 2008, 11:50am UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709 "2008-04-24T11:50:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 24, 2008, 11:50am UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709/1 "2008-04-24T11:50:12Z")

</div>

Hi,

Does anyone know how can I check if the data in one column of the table  
is with in the range of data in another column of another table?

Example:

Valid Numbers: 1 2 3 4

(number keyed in my number must be within the range of the valid  
numbers?)

(within range, data will be saved into the database)  
my number: 1 2 3

(not within range, a validation error box will pop up)  
my number: 7 8 9

Thanks

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 24, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709/2 "2008-04-24T20:12:40Z")

</div>

Perhaps:

validates\_inclusion\_of :numbers,  
:in =\> OtherTable.find(:all).map {|n| n.valid\_number}

…maybe. That’s if your OtherTable has columns id and valid\_number.  
If you could get valid number ranges from a non-database source, it  
might be easier:

VALID\_NUMBERS = 1…3  
validates\_inclusion\_of :numbers,  
:in =\> VALID\_NUMBERS

I haven’t tried this, so take it with a grain of salt.

-Kyle

On Apr 24, 4:50 am, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 25, 2008, 3:24am UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709/3 "2008-04-25T03:24:15Z")

</div>

Kyle wrote:

> Perhaps:
> 
> validates\_inclusion\_of :numbers,  
> :in =\> OtherTable.find(:all).map {|n| n.valid\_number}
> 
> …maybe. That’s if your OtherTable has columns id and valid\_number.  
> If you could get valid number ranges from a non-database source, it  
> might be easier:
> 
> VALID\_NUMBERS = 1…3  
> validates\_inclusion\_of :numbers,  
> :in =\> VALID\_NUMBERS
> 
> I haven’t tried this, so take it with a grain of salt.
> 
> -Kyle
> 
> On Apr 24, 4:50 am, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks Kyle. I used validates\_inclusion\_of :numbers, :in =\>  
OtherTable.find(:all).map {|n| n.valid\_number} in my model and it works.  
But this is not exactly what I was looking for. Is there a way to  
achieve the following example?

Example:

Valid Numbers: 1 2 4 8

(number keyed in my number must be within the range of the valid  
numbers? Meaning that if I key in numbers that are not in the valid  
numbers field, there will be an error)

(within range, data will be saved into the database)  
my number: 4 8

(not within range, a validation error box will pop up)  
my number: 3 7

Thanks

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 25, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709/4 "2008-04-25T19:23:35Z")

</div>

What does your valid numbers table look like? Is there one entry per  
valid number, or do you have one field with space-delimited values?  
What are the column names?

I don’t think that you mean “range” as in 1…8, unless I’m  
misunderstanding. You want to check to see if each digit (or number,  
if you’re allowing numbers over 9) is included in the valid numbers  
list, right?

How does the user enter a number? One at a time? Space-delimited?  
Single-digit only? In multiple fields?

What is being saved? One number? Anything enterd?

Perhaps giving the actual application of this would help me  
understand. It all makes a difference.

-Kyle

On Apr 24, 8:24 pm, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 27, 2008, 11:54am UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709/5 "2008-04-27T11:54:04Z")

</div>

Kyle wrote:

> What does your valid numbers table look like? Is there one entry per  
> valid number, or do you have one field with space-delimited values?  
> What are the column names?
> 
> I don’t think that you mean “range” as in 1…8, unless I’m  
> misunderstanding. You want to check to see if each digit (or number,  
> if you’re allowing numbers over 9) is included in the valid numbers  
> list, right?
> 
> How does the user enter a number? One at a time? Space-delimited?  
> Single-digit only? In multiple fields?
> 
> What is being saved? One number? Anything enterd?
> 
> Perhaps giving the actual application of this would help me  
> understand. It all makes a difference.
> 
> -Kyle
> 
> On Apr 24, 8:24 pm, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi,

My valid number is a field with space-delimited values. Yes your right.  
I want to check to see if each digit (or number, if you’re allowing  
numbers over 9) is included in the valid numbers list. When saved, the  
space-delimited values in the valid number field will be saved in to one  
column in the table.

Thanks

…first table…  
id(primary key) integer  
valid\_number integer  
…

…second table…  
id (primary key) integer  
my\_number integer  
…

…first model…

validates\_presence\_of :valid\_number  
validates\_numericality\_of :valid\_number

…

…second model…

validates\_presence\_of :my\_number  
validates\_numericality\_of :my\_number  
validates\_inclusion\_of :my\_number, :in =\> First.find(:all).map {|n|  
n.valid\_number}

…

…first controller…

def valid\_number  
@valid\_number=ValidNumber.new  
end

def save\_valid\_number  
@valid\_number=ValidNumber.new(params[:valid\_number])  
if @valid\_number.save  
redirect\_to :action =\> ‘list’  
flash[:notice] = ‘Valid number was successfully created.’  
else  
render :action =\> ‘valid\_number’  
end  
end

…

…second controller…

def my\_number  
@my\_number=MyNumber.new  
end

def save\_my\_number  
@my\_number=MyNumber.new(params[:my\_number])  
if @my\_number.save  
redirect\_to :action =\> ‘list’  
flash[:notice] = ‘My number was successfully created.’  
else  
render :action =\> ‘my\_number’  
end  
end

…

…first.rhtml…

\<%= error\_messages\_for ‘valid\_number’ %\>

**Create Valid Number**

\<% form\_tag({:action =\> :save\_valid\_number, :id =\> :valid\_number},  
{:onsubmit =\> ‘return validateValidNumberForm(this); return false;’}) do  
%\>

|
| |
| Valid Number: | \<%= text\_field :first ,:valid\_number %\> |

  
 \<%= button\_to "Create", :action =\> :save\_valid\_number, :id =\> :valid\_number %\> \<% end %\>

…

…second rhtml…

\<%= error\_messages\_for ‘my\_number’ %\>

**Create My Number**

\<% form\_tag({:action =\> :save\_my\_number, :id =\> :my\_number}, {:onsubmit  
=\> ‘return validateMyNumberForm(this); return false;’}) do %\>

|
| |
| My Number: | \<%= text\_field :second ,:my\_number %\> |

  
 \<%= button\_to "Create", :action =\> :save\_my\_number, :id =\> :my\_number %\> \<% end %\>

…

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 28, 2008, 4:51am UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709/6 "2008-04-28T04:51:17Z")

</div>

user splash wrote:

> Hi,
> 
> My valid number is a field with space-delimited values. Yes your right.  
> I want to check to see if each digit (or number, if you’re allowing  
> numbers over 9) is included in the valid numbers list. When saved, the  
> space-delimited values in the valid number field will be saved in to one  
> column in the table.
> 
> Thanks

Help needed.

Thanks

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [April 28, 2008, 5:27am UTC](https://www.ruby-forum.com/t/how-to-check-if-values-of-tables-are-within-range/135709/7 "2008-04-28T05:27:36Z")

</div>

OK, I don’t think that you’re actually using space-delimited values as  
the valid numbers; otherwise, :validates\_numericality\_of would fail  
(if you supplied it with “1 3 5 7 9” or something like that). Still,  
if that really is what you have, then you’ll have to change “1 3 5 7  
9” into an array:

valid\_numbers\_array = “1 3 5 7 9”.split(" ")

Of course, you’d have to get “1 3 5 7 9” through  
ValidNumber.find(:first) (if they’re all stored in ValidNumber id 1).

That sounds crazy to me, though, and I’m going to assume that you  
actually have a valid\_numbers table that looks like this:

id | valid\_number  
1 | 1  
2 | 3  
3 | 5  
4 | 7  
5 | 9

In that case, you’d get your array by doing  
ValidNumber.find(:all).map{|vn| vn.valid\_number}

If you supplied one number at a time into your “second rhtml” instead  
of putting “2 4 6 8 10”, you could check for valid numbers by doing:

validates\_inclusion\_of :my\_number, :in =\> ValidNumber.find(:all).map {|  
n| n.valid\_number}

Now, if you really want to be able to supply a bunch of space-  
delimited numbers and have them each checked against the valid  
numbers, you’ll have to iterate through each of them, and I don’t know  
if there’s an exact syntax for that. If the supplied numbers were “2  
4 6 8 10”, you would essentially have to do:

supplied\_numbers.split(" ").each {|sn| validates\_inclusion\_of sn, :in  
=\> ValidNumber.find(:all).map {|n| n.valid\_number}

But again, I don’t think that this is valid. I’m also not sure what  
you’re doing in your second rhtml file. If you’re working with a  
model, use form\_for, not form\_tag, and then use submit\_tag instead of  
button\_to. You shouldn’t be trying to pass the :id of the number that  
you haven’t created yet, either.

What are you really doing? Is this part of an app that has a bigger  
story or point, or is this an exercise to help you learn?

-Kyle

On Apr 27, 9:51 pm, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
