Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would recommend the following:</p> <p><strong>Developer is a User object.</strong> Distinguish developers from users with a is_developer boolean in your schema. This will make it easier going forward to keep Users / Developers integrated (without switch statements). You can add a named scope to find developers specfically:</p> <pre><code>class User &lt; ActiveRecord::Base named_scope :regular_users, :conditions =&gt; { :is_developer =&gt; false } named_scope :developers, :conditios =&gt; { :is_developer =&gt; true } #you can then call User.regular_users or User.developers end </code></pre> <p>Alternatively, you could have User / Developer work as polymorphic associations. E.g.</p> <pre><code>class Role &lt; ActiveRecord::Base belongs_to :entity, :polymorphic =&gt; true #with entity_id / entity_type in your schema end </code></pre> <p>The downside to this approach is it will make your code more complicated for little or zero semantic gain.</p> <hr> <p>I don't truly understand what you mean by default permission, but it seems to be a logic issue as opposed to a database. Does everyone have the default permission? Then you can add it on *after_create*, or when writing your logic, assume it's true (or controlled by a boolean flag). The following code will create a permission for each user that is default true after they are created (for existing users, you can add the permissions by hand / rake task).</p> <pre><code>class User &lt; ActiveRecord::Base after_create :add_default_permission def add_default_permission Permission.default_permissions.each do |permission| self.app_permissions.create(:permission_id =&gt; permission.id) end end end </code></pre> <p>As for default_permissions, I would suggest having an *is_default* boolean on the permissions table. This way, you can have multiple default permissions going forward (or remove default permissions later). As a default permission <strong>is a</strong> permissions, there's no need to differentiate the object models. I.e.</p> <pre><code>class Permission &lt; ActiveRecord::Base named_scope :default_permissions, :conditions =&gt; { :is_default =&gt; true } end </code></pre> <p>Finally, make sure to fully spell out all of your ActiveRecord associations, i.e.</p> <pre><code>class User &lt; ActiveRecord::Base has_many :apps has_many :permissions, :through =&gt; :app_permissions, :as =&gt; :permissible #edited end class App &lt; ActiveRecord::Base belongs_to :app_permission has_many :permissions, :through =&gt; :app_permissions, :as =&gt; :permissible #edited end class Permission &lt; ActiveRecord::Base belongs_to :app_permissions belongs_to :permissible, :through =&gt; :app_permissions, :polymorphic =&gt; true #edited end class AppPermission &lt; ActiveRecord::Base belongs_to :permissible, :polymorphic =&gt; true #edited belongs_to :app end </code></pre> <p>When a user installs an app: <strong>EDITED BELOW FOR POLYMORPHISM</strong></p> <pre><code>Class User &lt; ActiveRecord::Base def get_required_app(app) required_permissions = [] app.permissions.each do |p| if self.permissions.find(:first, conditions =&gt; { :permission_id =&gt; p.id } ).nil? required_permissions.push p end end required_permissions end def install_app(app) req = required_permissions app return req if req.count &gt; 0 #add user app end end </code></pre> <p>Hope this helps you work through your problem and let me know if you need any additional information.</p>
 

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