2

我有以下代码:

class Zombie < ActiveRecord::Base
  attr_accessible  :name, :rotting, :age
  has_many :assignments
  has_many :roles, through: :assignments
end

class Role < ActiveRecord::Base
  attr_accessible :title
  has_many :assignments
  has_many :zombies, through: :assignments
end

class Assignments < ActiveRecord::Base
  attr_accessible :role_id, :zombie_id
  belongs_to :zombie
  belongs_to :role
end

在控制台中,当我尝试运行此代码时:

zombie = Zombie.first
role = Role.first
zombie.assignments.create(role: role)

我收到以下错误:

NameError: uninitialized constant Zombie::Assignment.

我在这里犯了什么错误吗?

4

2 回答 2

4

Rails 模型是单数的,因此将类名更改AssignmentsAssignment.

于 2013-06-19T05:22:50.550 回答
0

尝试在您的控制台上运行以下代码

zombie = Zombie.first
zombie.roles << Role.find_by_title("Title")
zombie.roles
于 2013-06-19T06:00:16.187 回答