views:

22

answers:

1

How is it possible with active_record?

u = User.all
u = u.where(:id => 1)

NoMethodError: undefined method `where' for # u.class => Array

Can't chain conditions :(

+3  A: 

The all method executes the query. So you can't chain after using it.

u = User.where(:id => 1)
u.where(:id => 2)

This would execute the query WHERE id = 1 AND id = 2

Damien MATHIEU
thank u so much! is it possible to make .all query without executing it? )
nopolitica
You can use User.scoped: ree-1.8.7-2010.02 > User.scoped SQL (0.5ms) SHOW TABLES User Load (0.2ms) SELECT `users`.* FROM `users` => [#<User id: 1, email:.....
Dan McNevin
@nopolitica all queries are .all. They're restricted by the where previously provided before. So you can do : `User.where(:id => 1).all`
Damien MATHIEU
I just wanted to add that you still have the old finders available - User.find(params[:id]) isn't going anywhere, for example. It's only adding the hash of parameters (order, conditions, limit, etc) that are being deprecated. And remember that User.where(:id => 1) will return an array (like all the new query chains), not that single object.
Jaime Bellmyer