title: average_rating column for a Rating Model
body: <p>So I'm creating a rating (stars) Model:</p>
  
  <pre><code>  create_table "posts", force: true do |t|
      t.string   "title"
      t.text     "content"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.integer  "user_id"
      t.integer  "total_stars",   default: 0, null: false
      t.integer  "average_stars", default: 0, null: false
    end
  
    create_table "stars", force: true do |t|
      t.integer  "starable_id"
      t.string   "starable_type"
      t.integer  "user_id"
      t.integer  "number"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
  </code></pre>
  
  <p>So I already know how to get the total stars:</p>
  
  <p><strong>star.rb:</strong></p>
  
  <pre><code>  def add_to_total_stars
      if [Post].include?(starable.class)
        self.starable.update_column(:total_stars, starable.total_stars + self.number)
      end
    end
  </code></pre>
  
  <p>But for the average stars:</p>
  
  <pre><code>  def calculate_average_stars
      if [Post].include?(starable.class)
        self.starable.update_column(:average_stars, [SOMETHING MISSING HERE])
      end
    end 
  </code></pre>
  
  <p>I'm having some problems.</p>
  
  <p>I guess I have to create a column which will store all the <code>numbers</code> the post has so I can do something like this: </p>
  
  <pre><code>2 + 4 + 2 / total_stars
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>OK, I tried this:</p>
  
  <pre><code>  def calculate_average_stars
      if [Post].include?(starable.class)
        stars_list = self.starable.stars.map { |t| stars_list = t.number }   
        self.starable.update_column(:average_stars, stars_list.inject{ |sum, el| sum + el }.to_f / stars_list.size)
      end
    end
  </code></pre>
  
  <p>But I get this error:</p>
  
  <pre><code>1.9.3-p0 :010 &gt; star5.save
     (0.6ms)  begin transaction
    SQL (1.0ms)  UPDATE "posts" SET "total_stars" = 4 WHERE "posts"."id" = 5
    SQL (0.8ms)  UPDATE "posts" SET "average_stars" = NaN WHERE "posts"."id" = 5
  SQLite3::SQLException: no such column: NaN: UPDATE "posts" SET "average_stars" = NaN WHERE "posts"."id" = 5
     (0.6ms)  rollback transaction
  ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: NaN: UPDATE "posts" SET "average_stars" = NaN WHERE "posts"."id" = 5
  </code></pre>
  
  <p>Any suggestions of how to create such table?</p>