title: Rails error “NoMethodError” - Just started to use Rails
body: <p>Hopefully this is a very easy problem to solve as I am just starting out with Rails.</p>
  
  <p>I have one database (Products) which contains a number of product attributes including brand, colour and product_type.</p>
  
  <p>I currently have an index page (within productfinder directory) which is able to provide a summary of the products in the database.  I am using the following code in index.html.erb:</p>
  
  <pre><code>&lt;% @distinctbrands.each do |distinctbrand| %&gt;
    &lt;h4&gt;&lt;%= distinctbrand %&gt;&lt;/h4&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Within productfinder_controller.rb i have:</p>
  
  <pre><code> def index
      @distinctbrands = Product.find( :all, :order =&gt; 'brand ASC', :select =&gt; 'brand' ).map{ |i| i.brand }.uniq
      @distinctcolours = Product.find( :all, :order =&gt; 'colour ASC', :select =&gt; 'colour' ).map{ |i| i.colour }.uniq
    end
  </code></pre>
  
  <p>UP TO THIS POINT EVERYTHING SEEMS TO BE WORKING CORRECTLY</p>
  
  <p>Now I would like to add 3 buttons onto my index page, these buttons would enable the user to recalculate the distinctbrands and distinctcolours for a subset of the products - matching either product_type = "new" or "used".  The third button would correspond to the results not being filtered at all (i.e. as the index page loads initially).</p>
  
  <p>In a previous question I was advised to add the following code:
  In productfinder_controller:</p>
  
  <pre><code>before_filter :load_products, :only =&gt; [:index, :bybrand, :bycolour, :byprice]
  protected
  
   def load_products
      @products = Product.by_product_type(params[:product_type])
    end
  </code></pre>
  
  <p>In Product.rb:</p>
  
  <pre><code>scope :by_product_type, lambda{  |product_type| where(product_type: product_type.to_i) unless product_type.nil? }
  </code></pre>
  
  <p>In index.html.erb</p>
  
  <pre><code>&lt;%= link_to "New", :controller =&gt; params[:controller], :action =&gt; params[:action], :product_type =&gt; 'New' %&gt;
  &lt;%= link_to "Used", :controller =&gt; params[:controller], :action =&gt; params[:action], :product_type =&gt; 'Used' %&gt;
  &lt;%= link_to "Don't Restrict", :controller =&gt; params[:controller], :action =&gt; params[:action], :product_type =&gt; '' %&gt;
  </code></pre>
  
  <p>When the application is now run I get a NoMethodError - ProductFinder#Index (while trying to do nil.each
  The error is associated with &lt;% @distinctbrands.each do |distinctbrand| %></p>
  
  <p>I wondered if the problem was with the functions defining @distinctbrands and @distinctcolours as the database has now been filtered, however I get the same error for both the following situations:</p>
  
  <pre><code>def index
    @distinctbrands = @product.find( :all, :order =&gt; 'brand ASC', :select =&gt; 'brand' ).map{ |i| i.brand }.uniq
    @distinctcolours = @product.find( :all, :order =&gt; 'colour ASC', :select =&gt; 'colour' ).map{ |i| i.colour }.uniq
  end
  </code></pre>
  
  <p>and </p>
  
  <pre><code>def index
    @distinctbrands = Product.find( :all, :order =&gt; 'brand ASC', :select =&gt; 'brand' ).map{ |i| i.brand }.uniq
    @distinctcolours = Product.find( :all, :order =&gt; 'colour ASC', :select =&gt; 'colour' ).map{ |i| i.colour }.uniq
  end
  </code></pre>
  
  <p>Is somebody able to provide me with a possible solution please?  Is the problem to do with @products not being defined within def index?  If so, how can this be done? </p>
  
  <p>Many thanks for your time</p>