我对 ruby 有点陌生,我对 has_many :through 关联有很大的问题。我的系统当前设置了 Authlogic 和 Declarative_auth。在我提交用户的那一刻,它会正确创建所有内容,除了它不会在用户表中插入 role_id,即使它显示它在提交时被传递。它也不会将 id 保存在分配表中。首先,我想问题是,在 users 表中甚至需要 role_id 吗?其次,分配表中的 user_id 和 role_id 字段是否需要声明为 foreign_key 或者 rails 会自动处理这个?我很感激这方面的任何帮助。
class User < ActiveRecord::Base
acts_as_authentic
has_many :assignments
has_many :roles, :through => :assignments
def role_symbols
roles.map do |role|
role.name.underscore.to_sym
end
end
end
class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
end