0

I have a table in a Rails application where each column is of type String. This column, which will shall call mycolumn, is formatted such that each entry fits the format NN:NN where N is some digit from 0 - 9.

Now where I'm having trouble is I need to find all the elements such that mycolumn[0..1] is with in a certain range (lets just say 35).

I'm thinking the statement would look something like

Mytable.find(:all, :conditions => ['? <= 35', :mycolumn[0..1].to_i])

Would this work? Is there another way to do this?

1 Answer 1

1

No, that won't work that way --- you are going to have to either:

  1. format a SQL statement that works with the database you've chosen
  2. Use a datatype in your table that you can custom query on (like store both NN and NN in separate columns)
  3. Retrieve all models and do the select conditions in Ruby

I recommend #2 --- store the core data, and then add a method to format it to NN:NN

2
  • I was scared of that. Problem is the format I mentioned is how its going to be entered on the form. Jul 23, 2012 at 18:24
  • @LeeAbraham extract out what you need, store how you really need to access it, and present to the user later in the form they expect. Jul 23, 2012 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.