Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord where returns ActiveRecord::Relation

u = User.where("name = ?", "mateusz").limit(1)
u.class
=> ActiveRecord::Relation

So I cant do smth like u.email and so on. .find does right, returns User object. Is there any chance to get an User object from ActiveRecord::Relation object?

like image 662
matiit Avatar asked Dec 17 '22 22:12

matiit


1 Answers

You should call first or last on the ActiveRecord::Relation object:

u = User.where("name = ?", "mateusz").first
like image 115
alex.zherdev Avatar answered Jan 02 '23 03:01

alex.zherdev