1
我对ruby有点新鲜感,我对has_many有一个问题:通过关联。我的系统目前使用Authlogic和Declarative_auth进行设置。在我提交用户的那一刻,它创建了一切正确的东西,除非它不在用户表中插入role_id,即使它显示它在提交时传递。它也不会将ID保存在分配表中。首先,我想问题是,role_id在用户表中甚至是必需的吗?其次,赋值表中的user_id和role_id字段需要声明为foreign_key还是rails自动处理?我很感激这方面的帮助。has_many:通过关联问题
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
此外http://railscasts.com/episodes/188-claclarative-authorization – mark 2010-08-16 20:29:31