型号:
class Game < ActiveRecord::Base
belongs_to :manufacturer
end
class Manufacturer < ActiveRecord::Base
has_many :games
end
迁移:
class CreateManufacturers < ActiveRecord::Migration
def change
create_table :manufacturers do |t|
t.string :name
t.timestamps null: false
end
end
end
class CreateGames < ActiveRecord::Migration
def change
create_table :games do |t|
t.string :name
t.references :manufacturer, index: true, foreign_key: true
t.timestamps null: false
end
end
end
当我输入m = Game.find(2).manufacturer
时,这有效,并给我制造商名称。但是当我给g = Manufacturer.find(1).games
控制台抛出许多错误时。为什么has_many
不起作用?
或
如何获得第一家制造商开发的所有游戏?
显示错误:
NoMethodError: undefined method `games' for #<Manufacturer:0x007f996dc75368>