DEV Community

loading...

Executing an SQL Query Directly from ActiveRecord

rendegosling profile image Ren DG ・1 min read

You can execute sql queries directly from ActiveRecord

#execute

users = ActiveRecord::Base.connection.execute("SELECT id, first_name, created_at FROM users WHERE id=1")

users[0]
{
  "id"         => "1",
  "first_name" => "Ren",
  "created_at" => "2017-01-01 12:23:67.425118"
}
Enter fullscreen mode Exit fullscreen mode

Discussion (1)

Collapse
igorkasyanchuk profile image
Forem Open with the Forem app