我正在使用rails 4.1.12并最近尝试创建has_many_through关联。
我的榜样
# == Schema Information
#
# Table name: roles
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
#
class Role < ActiveRecord::Base
has_many :users
has_many :roles_responsibilities
has_many :responsibilities, through: :roles_responsibilities
end
责任模型
# == Schema Information
#
# Table name: responsibilities
#
# id :integer not null, primary key
# action_name :string(255)
# controller_name :string(255)
# created_at :datetime
# updated_at :datetime
#
class Responsibility < ActiveRecord::Base
has_many :roles_responsibilities
has_many :roles, through: :roles_responsibilities
end
和
Roles_responsibilities
# == Schema Information
#
# Table name: roles_responsibilities
#
# id :integer not null, primary key
# role_id :integer
# responsibility_id :integer
# user_id :integer
# user_type :string(255)
# created_at :datetime
# updated_at :datetime
#
class RolesResponsibility < ActiveRecord::Base
belongs_to :role
belongs_to :responsibility
end
我相信我做得不对,但得到了一个奇怪的
NameError: undefined local variable or method ` ' for Role (call 'Role.connection' to establish a connection):Class
在模特中。
我检查了所有拼写和语法,但不确定为什么我收到此错误?
任何人都可以帮我找到错误吗?
答案 0 :(得分:0)
这是因为ActiveRecord为您的数据库连接延迟加载。如果再次运行查询,它应该可以正常工作,根据this SO问题。您可以通过运行Role.count