views:

56

answers:

3

For some reason, the food_id field in 'ratings' isn't populating when I run the seed.rb file below. Can somebody help me figure out why?

Seed file contains the following lines:

Food.create(:id => 1, :description => 'Stonyfield Farm Yomommy 4oz. Strawberry')
OverallRating.create(:score => 0, :count => 1, :food_id => 1)

Code for Food and Rating are as follows: class OverallRating < Rating belongs_to :food end

class Food < ActiveRecord::Base
   has_one :overall_rating
end

class Rating < ActiveRecord::Base
  belongs_to :food
end

The rating migration file is as follows:

class CreateRatings < ActiveRecord::Migration
  def self.up
    create_table :ratings do |t|
      t.integer :food_id
      t.integer :count
      t.decimal :score
      t.string :type
      t.timestamps
    end
  end

  def self.down
    drop_table :ratings
  end
end
A: 

how are you invoking the seeds.rb file? You may have to do a rake db:seed

Matt Briggs
i invoke:rake db:migrate:resetrake db:seedand the result is the same
Tian
A: 

Is that really the actual code? I would guess you've got an attr_accessible or attr_protected declaration in Rating/OverallRating that is preventing the option from being set in the instantiated object.

Winfield
yes this is the actual code. I removed all attr_accessible and attr_protected declarations and it still doesn't update...
Tian
Why does this code reference OverallRating when the model here is called Rating?Can you post the live db schema for these two tables?
Winfield
A: 

Instead of hardcoding the id, try something like this:

food = Food.create(:description => 'blah')
food.create_overall_rating(:score => 0, :count => 1)

I just tried your exact method though, and it worked for me without a problem. So perhaps you have something else awry.

Aaron Hinni