title: How to properly write SQL queries in Rails
body: <p>I have a database with two tables: <code>users</code> and <code>posts</code> where one user can have many posts.</p>
  
  <p>I want to use plain SQL instead of ActiveRecord in some situations since plain SQL is faster.</p>
  
  <p>There is a method <code>index</code> in my UsersController that looks like this:</p>
  
  <pre><code>def index
      @users = User.get_users_and_posts
  end
  </code></pre>
  
  <p>This method returns users who have posts and returns their posts.
  The <code>get_users_and_posts</code> method is implemented in my model:</p>
  
  <pre><code>class User &lt; ApplicationRecord
  ...
   private
  
   def self.get_users_and_posts
     ActiveRecord::Base.connection.execute("
       SELECT
         users.name, users.email, posts.title, posts.body
       FROM
         users
       INNER JOIN
         posts
       ON
         users.id = posts.user_id")
   end
  </code></pre>
  
  <p>Is this the proper way to write SQL queries in Rails? Does it follow the Rails Way?</p>