How to check if values of tables are within range?

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

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

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 <rails-mailing-l...@andreas-s.net>

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

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

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 <rails-mailing-l...@andreas-s.net>

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' %>

<table border="1px"> <b><u>Create Valid Number</u></b><p>

<% form_tag({:action => :save_valid_number, :id => :valid_number}, {:onsubmit => 'return validateValidNumberForm(this); return false;'}) do %>

  <tr> <tr><td style="width:300px; height: 8px;colspan="1" rowspan="1">Valid Number: <td style="width: 200px; height: 8px;colspan="1" rowspan="1"><%= text_field :first ,:valid_number %></td> </tr>

</table><br/>   <%= button_to "Create", :action => :save_valid_number, :id => :valid_number %> <% end %>

..............................

......second rhtml............

<%= error_messages_for 'my_number' %>

<table border="1px"> <b><u>Create My Number</u></b><p>

<% form_tag({:action => :save_my_number, :id => :my_number}, {:onsubmit => 'return validateMyNumberForm(this); return false;'}) do %>

  <tr> <tr><td style="width:300px; height: 8px;colspan="1" rowspan="1">My Number: <td style="width: 200px; height: 8px;colspan="1" rowspan="1"><%= text_field :second ,:my_number %></td> </tr>

</table><br/>   <%= button_to "Create", :action => :save_my_number, :id => :my_number %> <% end %>

..............................

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

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