Working Title
Working Title

Reputation: 254

Conditions and variables in .find() statements

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?

Upvotes: 0

Views: 73

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

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

Upvotes: 1

Related Questions