title: Saving a model with has_many association
body: <p>I am building a review application for schools. A user can only review a school if he is logged in.</p>
  
  <p><strong>What you can do right now:</strong></p>
  
  <p>You can write a review and add a rating between 1 - 5. The application saves the review. </p>
  
  <p><strong>What I want it do:</strong></p>
  
  <p>I would like to save the the logged user_id. So a review belongs to a user and a school.</p>
  
  <p>But I am kinda confused on how to do that. Any ideas and guidelines are welcome =)</p>
  
  <p><strong>Review model:</strong></p>
  
  <pre><code>class Review &lt; ActiveRecord::Base
    belongs_to :school
    belongs_to :user
    attr_accessible :content, :rating
  end
  </code></pre>
  
  <p><strong>School model:</strong></p>
  
  <pre><code>class School &lt; ActiveRecord::Base
    has_many :reviews
    ..more code..
  end
  </code></pre>
  
  <p><strong>User model:</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base  
    has_many :reviews
    ...more code..
  end
  </code></pre>
  
  <p><strong>The review controller:</strong></p>
  
  <pre><code>class ReviewsController &lt; ApplicationController
      def create
          @school = School.find(params[:school_id])
          @review = @school.reviews.create!(params[:review])
          redirect_to @review.school, notice: "Review has been created."
      end
  end
  </code></pre>
  
  <p><strong>The review form</strong></p>
  
  <pre><code>&lt;%= form_for [@school, Review.new] do |f| %&gt;
          &lt;h3&gt;Write a review&lt;/h3&gt;
          &lt;div class="reviews_comments_container"&gt;
              &lt;div class="review_box"&gt;
                  &lt;ol class="radio-rater"&gt;
                      &lt;%= f.radio_button(:rating, 1) %&gt;
                      &lt;%= f.radio_button(:rating, 2) %&gt;
                      &lt;%= f.radio_button(:rating, 3) %&gt;
                      &lt;%= f.radio_button(:rating, 4) %&gt;
                      &lt;%= f.radio_button(:rating, 5) %&gt;
                  &lt;/ol&gt;
              &lt;/div&gt;
              &lt;div class="review_box" id="review_textarea"&gt;
                  &lt;%= f.text_area :content, rows: 4, cols: 70 %&gt;
              &lt;/div&gt;
              &lt;div class="review_box"&gt;
                  &lt;%= f.submit 'Save your review', :class =&gt; "btn" %&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p><strong>The migration file:</strong></p>
  
  <pre><code>class CreateReviews &lt; ActiveRecord::Migration
    def change
      create_table :reviews do |t|
        t.text :content
        t.integer :rating
        t.belongs_to :school
        t.belongs_to :user
        t.timestamps
      end
      add_index :reviews, [:school_id, :user_id]
    end
  end
  </code></pre>