我在不同(并且有些关联)模型上有几个范围
class Alarm
has_one :site #use mac_address field in Alarm and Site as key / foreign key
scope :any_network, lambda{ joins(:network) } #more complex stuff can be done :-)
#etc
end
class Network
has many :permissions
has_many :sites
scope :has_permission, lambda{|level, user| #etc - returns a list of networks that a
# user has the `level` permission for }
#etc
end
class Permission
has_one :network, :user
#etc -uses a flags field to hold permision levels
end
所以我可以
Alarm.any_network
这会产生
SELECT `alarm`.* FROM `alarm` INNER JOIN `site` ON
`site`.`mac_address` = `alarm`.`mac_address` INNER JOIN `network` ON
`network`.`id` = `site`.`network_id`
我能做到
Network.has_permission('manager',1)
这会产生
SELECT `network`.* FROM `network` INNER JOIN `permission` ON
`permission`.`network_id` = `network`.`id`
WHERE (permission.user_id = 1 and permission.flags && 8)
这就是它应该的样子。
我无法解决的是如何加入这两个范围以生成源自用户具有适当权限的任何网络的所有警报的集合。类似的东西
SELECT `alarm`.* FROM `alarm` INNER JOIN `site`
ON `site`.`mac_address` = `alarm`.`mac_address`
INNER JOIN `network` ON `network`.`id` = `site`.`network_id`
INNER JOIN `permission` ON `permission`.`network_id` = `network`.`id`
WHERE (permission.user_id = 1 and permission.flags && 8)
我尝试链接范围(无法使其工作)并且我尝试合并范围(显然是错误的做法!)
任何想法——这几天我一直在扯头发。
谢谢,
史蒂夫