如何让has_many工作?

时间:2015-09-17 05:21:13

标签: ruby-on-rails model

型号:

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>

0 个答案:

没有答案