I love new Rail 3!
The new query syntax is so awesome:
users = User.where(:name => 'Bob', :last_name => 'Brown')
But when we need to do something like
SELECT * FROM Users WHERE Age >= const AND Money > const2
We have to use
users = User.where('Age >= ? and money > ?', const, const2)
Which is not very cool. The following query is not safe because of SQL injection:
users = User.where('Age >= #{const} and money > #{const2}')
I like the C#/LINQ version
var users = DB.Where(u => u.Age >= const && u.Money > const2);
Is there a way to do something like that in Rails?
The new querying with rails isn’t vulnerable to SQL injection. Any quotes in the argument are escaped.
Rails 3 AR has gained the delayed execution that LINQ has had for a while. This lets you chain any of the query methods. The only time you have to put 2 or more parts into a where is when you want an
OR.That aside, there are many different ways to do your query.