Suppose I have a model User
User.find(id)
Returns a row where primary key = id. The return type will be User
object.
User.find_by(email:"[email protected]")
Returns first row with matching attribute or email in this case. Return type will be User
object again.
Note :- User.find_by(email: "[email protected]")
is similar to User.find_by_email("[email protected]")
User.where(project_id:1)
Returns all users in users table where attribute matches.
Here return type will be ActiveRecord::Relation
object. ActiveRecord::Relation
class includes Ruby's Enumerable
module so you can use it's object like an array and traverse on it.