RSS

Category Archives: SQL

Rails SQL query

I needed to know the highest single (integer) value from a table:column. Not having done this before in Rails, my first intuition was to write something that would create a connection with the db, run a SQL string in a query, return the results to a variable, look in the variable if it came back as an array or some other object and get my value.

As always, I was pleasantly surprised to find that I could run a single query and get my value in a very readable, succinct statement:

@total_pages = ActiveRecord::Base.connection.select_value('SELECT MAX(page) FROM questions')

Beautiful.

 
1 Comment

Posted by on April 25, 2007 in Rails, Ruby, SQL

 
 
Follow

Get every new post delivered to your Inbox.