title: displaying records in ruby on rails using a conditional filter breaking down
body: <p>How can I display all the records in a table if there is no <code>params[:subject_id]</code> sent, or filter the records that it will display if the <code>params[:subject_id]</code> is sent?.</p>
  
  <p>So far I have a before filter in the pages controller to check if the <code>params[:subject_id]</code> were sent and creates an instance variable.</p>
  
  <pre><code>  private
  
    def find_subject
      if params[:subject_id]
        @subject = Subject.find_by_id(params[:subject_id])
      else
        @subject = Subject.all
      end
    end
  </code></pre>
  
  <p>problem is in the action when done like this <code>@pages</code> only stores the last record but won't break anything in the view.</p>
  
  <pre><code>  before_filter :find_subject
  
    def list
      @subject.each do |subject|
        @pages = Page.order("pages.position ASC").where(:subject_id =&gt; subject.id)
      end
    end
  </code></pre>
  
  <p>however if i do it like this </p>
  
  <pre><code>  def list
        @pages = Page.order("pages.position ASC").where(:subject_id =&gt; @subject.id)
    end
  </code></pre>
  
  <p>no records are returned the returned at all, when i check the output in the webrick console I see this</p>
  
  <pre><code>Subject Load (0.2ms)  SELECT `subjects`.* FROM `subjects` 
  Page Load (0.4ms)  SELECT `pages`.* FROM `pages` WHERE `pages`.`subject_id` = 2186859380 ORDER BY pages.position ASC
  </code></pre>
  
  <p>Im not sure how its getting that id of <code>2186859380</code></p>
  
  <p>I have also tired it this way </p>
  
  <pre><code>  def list
      if @subject.object_id.nil?
        @pages = Page.order("pages.position ASC")
      else
        @pages = Page.order("pages.position ASC").where(:subject_id =&gt; @subject.object_id)
      end
    end
  </code></pre>
  
  <p>however this also throws some errors and breaks my view as well.</p>
  
  <p>here is my view so you can get a complete picture</p>
  
  <pre><code>&lt;div class="page list"&gt;
      &lt;h2&gt;Pages&lt;/h2&gt;
  &lt;%= link_to "Add a new Page", {:action =&gt; "new", :subject_id =&gt; @subject.id}, :class =&gt; "action new" %&gt;
      &lt;table class="listing" summary="Pages List"&gt;
          &lt;tr class="header"&gt;
              &lt;th&gt;&amp;nbsp;&lt;/th&gt;
              &lt;th&gt;Subject&lt;/th&gt;
              &lt;th&gt;Page&lt;/th&gt;
              &lt;th&gt;Visible&lt;/th&gt;
              &lt;th&gt;Sections&lt;/th&gt;
              &lt;th&gt;Permalink&lt;/th&gt;
              &lt;th&gt;Actions&lt;/th&gt;
          &lt;/tr&gt;
          &lt;% @pages.each do |page| %&gt;
          &lt;tr class="&lt;%= cycle("odd", "even") %&gt;"&gt;
              &lt;td&gt;&lt;%= page.position %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= page.subject.name %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= page.name %&gt;&lt;/td&gt;
              &lt;td class="center"&gt;&lt;%= page.visible ? "Yes" : "No" %&gt;&lt;/td&gt;
              &lt;td class="center"&gt;&lt;%= page.sections.size %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= page.permalink %&gt;&lt;/td&gt;
              &lt;td class="actions"&gt;
                  &lt;%= link_to("Sections", {:controller =&gt; "sections", :action =&gt; "list", :page_id =&gt; page.id}, :class =&gt; "action delete")%&gt;
                  &lt;%= link_to("Show", {:action =&gt; "show", :id =&gt; page.id, :subject_id =&gt; @subject.id}, :class =&gt; "action show")%&gt;
                  &lt;%= link_to("Edit", {:action =&gt; "edit", :id =&gt; page.id, :subject_id =&gt; @subject.id}, :class =&gt; "action edit")%&gt;
                  &lt;%= link_to("Delete", {:action =&gt; "delete", :id =&gt; page.id, :subject_id =&gt; @subject.id}, :class =&gt; "action delete")%&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;% end %&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Any help on what I am doing wrong would be greatly appreciated.</p>