Kirill Salykin
Kirill Salykin

Reputation: 711

ActiveRecord where similar to find

Rails have method where which returns Relation and find method which accepts entity and looks based on primary (you shouldn't specify it explicit).

Is there any where method which accepts entity?

eg, User.where(params[:id]) -> select .. from users where user.id = ...

Upvotes: 0

Views: 132

Answers (2)

Josh Rieken
Josh Rieken

Reputation: 2266

If you already have the user record, why do you need to find it again using where? If it's an association, you can do something like User.where(:company_id => company.id), but you can't pass the record itself.

Also,User.find(params[:id]) is the same as User.where(:id => params[:id]).first.

Upvotes: 1

Marcel Hebing
Marcel Hebing

Reputation: 3182

The following works, but I don't understand why you are looking for an object you already have?

User.where(:id => user)

Upvotes: 1

Related Questions