Rails DB Seeding
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 b开发者_如何学JAVAelongs_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
how are you invoking the seeds.rb file? You may have to do a rake db:seed
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.
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.
精彩评论