2

我有以下

has_many :administrators, :class_name => "User", :conditions => "role_id = 4"

它工作正常,但不是使用外键

"role_id = 4"

我更愿意在角色表中指定与该外键相关的实际角色字符串,例如“Admin”

更新:

SELECT *
FROM users u, roles r
WHERE u.role_id = r.id
AND r.role = "Admin"

更新 2

我不能做这样的事情吗:(这不起作用,但说明了我正在尝试做的事情)

has_many :administrators, :class_name => "User", :conditions => { :role => {:name => "Admin"}}
4

2 回答 2

2

弄清楚了:

has_many :administrators, :class_name => "User", :conditions => {:roles => {:name => "Admin"}}, :include => :role
于 2011-05-26T13:32:46.580 回答
0

试试怎么样

has_many :administrators, :class_name => "User", :conditions => "role_id = #{Role.find(:name => 'Admin')}" 

假设角色表有对应的模型。您使用的是特定框架还是自制授权?

于 2011-05-26T08:31:31.350 回答