在 Ruby on Rails 中指定关系的条件
我有以下内容
has_many :administrators, :class_name => "User", :conditions => "role_id = 4"
,它工作正常,但我不想使用外键,而是
"role_id = 4"
更愿意指定角色表中与该外键相关的实际角色字符串,例如“Admin”
UPDATE:
SELECT *
FROM users u, roles r
WHERE u.role_id = r.id
AND r.role = "Admin"
UPDATE 2
我不能这样做像这样的东西:(这不起作用,但说明了我正在尝试做的事情)
has_many :administrators, :class_name => "User", :conditions => { :role => {:name => "Admin"}}
I have the following
has_many :administrators, :class_name => "User", :conditions => "role_id = 4"
and it works fine, but instead of using the foreign key
"role_id = 4"
I would prefer to specify the actual role string in the roles table, that that foreign key relates to, e.g "Admin"
UPDATE:
SELECT *
FROM users u, roles r
WHERE u.role_id = r.id
AND r.role = "Admin"
UPDATE 2
can't I do something like this: (this doesn't work, but illustrates what I am trying to do)
has_many :administrators, :class_name => "User", :conditions => { :role => {:name => "Admin"}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了:
Figured it out:
尝试一下
假设角色表有一个相应的模型。您使用的是特定框架还是自制授权?
what about trying
Assuming the role table had a corresponding model. are you using a specific framework, or home baked authorization?