title: Ruby on rails: Trouble creating an object with multiple associations
body: <p>I am having a problem with creating an object with an association.</p>
  
  <p>I have a Message model that belongs_to a job, and a user or runner. Inside my jobs/index.html I want to show a list of jobs with their corresponding messages and a form to create a new message for that particular job.</p>
  
  <p>The problem is whenever I create a message, job_id stays nil. I am new to ruby on rails, so I still dont fully understand this stuff.</p>
  
  <p>Here is part of my jobs/index.html (NOTE: not my actual code, I am in class so I just typed this up, may contain syntax errors).</p>
  
  <pre><code>    &lt;% @jobs.each do |job| %&gt;
  
        &lt;p&gt; &lt;%= job.body %&gt; &lt;/p&gt;
  
        &lt;%= form_for job.messages do |f| %&gt;
          &lt;%= f.label :body %&gt;
          &lt;%= f.text_field :body %&gt;
          &lt;%= f.submit %&gt;
        &lt;% end %&gt;
  
        &lt;%if job.messages.present? %&gt;
        &lt;ul&gt;
          &lt;% job.messages.each do |message| %&gt;
              &lt;li&gt;
                  &lt;p&gt; message.description &lt;p&gt;
              &lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
        &lt;% else %&gt;
          &lt;p&gt; No messages &lt;p&gt;
        &lt;% end %&gt;
  
      &lt;% end %&gt;
  </code></pre>
  
  <p>Here is the create method in message controller (NOTE: current_login can be a runner or user, they both share the same attributes)</p>
  
  <pre><code>def create
      @message = current_login.messages.new(params[:message])
      @message.save
  end
  </code></pre>
  
  <p>Job controller index action</p>
  
  <pre><code>def index
      @jobs = Job.all
  end
  </code></pre>
  
  <p>Message model</p>
  
  <pre><code>class Message &lt; ActiveRecord::Base
    attr_accessible :description
    belongs_to :user
    belongs_to :runner
    belongs_to :job
  end
  </code></pre>
  
  <p>User model</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    attr_accessible :username
    has_many :jobs
  end
  </code></pre>
  
  <p>Runner model</p>
  
  <pre><code>class Runner &lt; ActiveRecord::Base
    attr_accessible :username
    has_many :jobs
  end
  </code></pre>
  
  <p>Job model</p>
  
  <pre><code>class Job &lt; ActiveRecord::Base
    attr_accessible :body
    has_many :messages
    belongs_to :user
    belongs_to :runner
  end
  </code></pre>
  
  <p>Whenever I submit the message form inside the jobs/index.html view, it seems to successfully create a message with user_id or runner_id successfully filled out (depending on who posted the message), but I am getting nil for the job_id attribute.</p>