1

There are two ways that I know that you can fetch results in Rails through AR, here are two example queries that return the same results but make different SQL queries.

A) where("created_at < ? and created_at > ?", week1, week2)

B) where(:created_at => week1..week2)

The second one (besides being nicer looking, IMHO) also creates a BETWEEN type SQL query, while the first one is pretty much self explanatory.

Which one is better performing, or better for any other reason besides style?

Victor S
  • 5,098
  • 5
  • 44
  • 62

2 Answers2

1

I think B is what most people would go for because they write less code and don't have to think about SQL query at all.

For performance, I think it should be the same. MySQL just provides a nicer way to do a where clause in a range value.

Chamnap
  • 4,666
  • 2
  • 34
  • 46
0

it would be little bit db specific ..as rails generate these queries for them

A) WHERE (created_at < '2012-05-29 13:14:50' and created_at > '2012-05-28 15:14:50')

B) WHERE (`questions`.`created_at` BETWEEN '2012-05-28 15:14:39' AND '2012-05-29 13:14:39')

:) You should not worry much about it ...as long as you are using ActiveRecord

have a look at this

Community
  • 1
  • 1
Amol Pujari
  • 2,280
  • 20
  • 42