title: Optimize ruby on rails SQL queries
body: <p><strong>Update start</strong></p>
  
  <p>This problem was with rendering a form for each project, not with SQl queries. To optimize I will add the form as needed with javascript.</p>
  
  <p>It seems like I didn't read the miniprofiler log correct. I apologize, but leave the question for others that might have similar questions. </p>
  
  <p><strong>Update end</strong></p>
  
  <p>I am using miniprofiler to find bottlenecks in my app. And I found one!</p>
  
  <pre><code>SELECT "projects".* FROM "projects" INNER JOIN "memberships" ON 
  "projects"."id" = "memberships"."project_id" WHERE 
  "memberships"."user_id" = 1 AND (active = 't')   
  1059.50 ms  
  Rendering: projects/_index — 1023.18 ms
  </code></pre>
  
  <p>It is finding 185 projects in 1 whole second.</p>
  
  <p><strong>How can I make a query that does this more efficient?</strong></p>
  
  <p>I have this in my projects_controller index</p>
  
  <pre><code>@projects = current_user.projects.is_active
  </code></pre>
  
  <p>The is_active scope in the projects model</p>
  
  <pre><code>scope :is_active, where(["active = ?", true])
  </code></pre>
  
  <p>The projects and users has a many to many relationship, with a membership join table</p>
  
  <p>The membership model </p>
  
  <pre><code>class Membership &lt; ActiveRecord::Base
    attr_accessible :project_id,:user_id,:created_at,:updated_at
    belongs_to :user
    belongs_to :project
  end
  </code></pre>
  
  <p>The membership table</p>
  
  <pre><code>def self.up
      create_table :memberships do |t|
        t.integer :project_id
        t.integer :user_id
  
        t.timestamps
      end
      add_index :memberships, [:project_id, :user_id], :unique =&gt; true
  end
  </code></pre>
  
  <p>I am running this in the production environment on the local computer with postgreSQL as the database</p>
  
  <p>Adding explain by Jiří Pospíšil request. In the console it does not seem to be slow at all. This explain is done in development. Have the same problem there</p>
  
  <pre><code>User.first.projects.is_active.explain
    User Load (0.3ms)  SELECT "users".* FROM "users" LIMIT 1
    Project Load (2.3ms)  SELECT "projects".* FROM "projects" INNER JOIN "memberships" ON "projects"."id" = "memberships"."project_id" WHERE "memberships"."user_id" = 1 AND (active = 't')
    EXPLAIN (0.2ms)  EXPLAIN QUERY PLAN SELECT "projects".* FROM "projects" INNER JOIN "memberships" ON "projects"."id" = "memberships"."project_id" WHERE "memberships"."user_id" = 1 AND (active = 't')
   =&gt; "EXPLAIN for: SELECT \"projects\".* FROM \"projects\" INNER JOIN \"memberships\" ON \"projects\".\"id\" = \"memberships\".\"project_id\" WHERE \"memberships\".\"user_id\" = 1 AND (active = 't')\n0|0|1|SEARCH TABLE memberships USING INDEX index_memberships_on_user_id (user_id=?) (~10 rows)\n0|1|0|SEARCH TABLE projects USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)\n" 
  </code></pre>
  
  <p>The view</p>
  
  <pre><code>&lt;% @projects.each do |project| %&gt;
  &lt;li class="tab_list" id="project_&lt;%= project.id.to_s %&gt;"&gt; 
    &lt;div class="tab_list_text"&gt;&lt;%= link_to project.name, project_path(project) %&gt;&lt;/div&gt;
    &lt;span class='open_project_update button edit' id="project_update" data-id="&lt;%= project.id %&gt;" data-object="project" title="Edit project"&gt;Edit&lt;/span&gt; 
    &lt;div class="dialog_form" id="project_update_&lt;%= project.id %&gt;_form" title="Update project" style="display:none;"&gt;
        &lt;%= form_for(project) do |f| %&gt;
        &lt;ul&gt;
        &lt;li&gt;&lt;%= f.label :name %&gt;&lt;%= f.text_field :name %&gt;&lt;/li&gt;
        &lt;li&gt;&lt;%= f.label :description %&gt;&lt;%= f.text_field :description %&gt;&lt;/li&gt;
        &lt;li&gt;&lt;%= f.label :due %&gt;&lt;%= f.text_field :due, :value =&gt; project.due.strftime("%Y-%m-%d"), :id =&gt; "date_project_#{project.id}"  %&gt;&lt;/li&gt;
        &lt;li&gt;&lt;%= f.label :customer_id %&gt;&lt;%= f.select(:customer_id, @customers.map {|customer| [customer.name, customer.id]}, {:include_blank =&gt; 'None'})%&gt;&lt;/li&gt;
        &lt;li&gt;&lt;%= f.submit 'Save', :class =&gt; 'submit' %&gt;&lt;/li&gt;&lt;/ul&gt;
        &lt;% end %&gt;
    &lt;/div&gt;
    &lt;a class="activate_project button" data-object="project" data-id="&lt;%= project.id.to_s %&gt;"&gt;Archive&lt;/a&gt;
  &lt;/li&gt;
  &lt;% end %&gt;
  </code></pre>