title: Rails Query: Undefined Method for nil:NilClass
body: <p>In my Rails app, I have projects that have many steps, and each step has many images.</p>
  
  <p>I'm trying to tag links to pages about each step with information about the step, particularly the name of the step and the default image associated with the step.</p>
  
  <pre><code>&lt;%= link_to "", project_step_path(@project, i), :class=&gt; "dot", 
  data: {title: steps.find_by_number(i).name, 
  image: steps.find_by_number(i).images.order("position ASC").first.file} %&gt;
  </code></pre>
  
  <p>The title data tag does return the name of the step, but I'm getting an error with my image data tag. When I try it in the rails console, it returns the file path to the image, but when I try to implement it in my app, I get the error <strong>undefined method `file' for nil:NilClass</strong>.  </p>
  
  <p>When I remove "file" from my image tag (so it's image: steps.find_by_number(i).images.order("position ASC").first), and I return what the image data tag is for each link, I get [object Object].  </p>
  
  <p>How can I get it to return the right query result?</p>
  
  <p>Here is my steps controller:</p>
  
  <pre><code>class StepsController &lt; ApplicationController
  
    before_filter :get_project
  
    def show
      @step = @project.steps.find_by_number(params[:id])
      @image = Image.new
      @images = @step.images.order("position")
      @steps = @project.steps.order("number")
      @numSteps = @steps.count
  
      respond_to do |format|
        format.html # show.html.erb
        format.json { render :json =&gt; @step }
      end
    end
    private
    # get_project converts the project_id given by the routing
    # into an @project object
    def get_project
      @project = Project.find(params[:project_id])
    end
  end
  </code></pre>