Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - combining scopes from different tables
    primarykey
    data
    text
    <p>I have a couple of scopes on different (and somewhat linked) models</p> <pre><code>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 </code></pre> <p>So I can do </p> <pre><code>Alarm.any_network </code></pre> <p>which generates</p> <pre><code>SELECT `alarm`.* FROM `alarm` INNER JOIN `site` ON `site`.`mac_address` = `alarm`.`mac_address` INNER JOIN `network` ON `network`.`id` = `site`.`network_id` </code></pre> <p>and I can do</p> <pre><code>Network.has_permission('manager',1) </code></pre> <p>which generates</p> <pre><code>SELECT `network`.* FROM `network` INNER JOIN `permission` ON `permission`.`network_id` = `network`.`id` WHERE (permission.user_id = 1 and permission.flags &amp;&amp; 8) </code></pre> <p>and this is all as it should be.</p> <p>What I can't work out is how to join these two scopes to generate the set of all alarms originating from any network which the user has the appropriate permission for. Something along the lines of</p> <pre><code>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 &amp;&amp; 8) </code></pre> <p>I've tried chaining the scopes (can't get it to work) and I have tried merging the scopes (clearly the wrong thing to do!)</p> <p>Any ideas - I've been tearing my hair out on this one for a couple of days.</p> <p>Thanks,</p> <p>Steve</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload