ruby on rails - Database Model and associations solution -
ruby on rails - Database Model and associations solution -
i have models
user, developer, app, permission
a user can have default permision a user can have permission different application a user can install multiple applications a user can developer of application a developer still user , can have user's privillage (install applcation, default permission, permissions each application) until have:
user.rb
class user < activerecord::base has_many :apps end
app.rb
class app < activerecord::base has_many :permissions, :through => :app_permissions end
permission.rb
class permission < activerecord::base belongs_to :app end
app_permission.rb
class apppermission < activerecord::base end
questions
how distinguish users? (regular, developer) improve utilize cancan or rails sti or simple roles class? please justify why improve utilize of 3 solutions or else. is improve create default_permission model separate application permissions default permission?edit:
if miss info please ask. see different solutions , how each solution works. thanks
i recommend following:
developer user object. distinguish developers users is_developer boolean in schema. create easier going forwards maintain users / developers integrated (without switch statements). can add together named scope find developers specfically:
class user < activerecord::base named_scope :regular_users, :conditions => { :is_developer => false } named_scope :developers, :conditios => { :is_developer => true } #you can phone call user.regular_users or user.developers end
alternatively, have user / developer work polymorphic associations. e.g.
class role < activerecord::base belongs_to :entity, :polymorphic => true #with entity_id / entity_type in schema end
the downside approach create code more complicated little or 0 semantic gain.
i don't understand mean default permission, seems logic issue opposed database. have default permission? can add together on *after_create*, or when writing logic, assume it's true (or controlled boolean flag). next code create permission each user default true after created (for existing users, can add together permissions hand / rake task).
class user < activerecord::base after_create :add_default_permission def add_default_permission permission.default_permissions.each |permission| self.app_permissions.create(:permission_id => permission.id) end end end
as default_permissions, suggest having *is_default* boolean on permissions table. way, can have multiple default permissions going forwards (or remove default permissions later). default permission is a permissions, there's no need differentiate object models. i.e.
class permission < activerecord::base named_scope :default_permissions, :conditions => { :is_default => true } end
finally, create sure spell out of activerecord associations, i.e.
class user < activerecord::base has_many :apps has_many :permissions, :through => :app_permissions, :as => :permissible #edited end class app < activerecord::base belongs_to :app_permission has_many :permissions, :through => :app_permissions, :as => :permissible #edited end class permission < activerecord::base belongs_to :app_permissions belongs_to :permissible, :through => :app_permissions, :polymorphic => true #edited end class apppermission < activerecord::base belongs_to :permissible, :polymorphic => true #edited belongs_to :app end
when user installs app: edited below polymorphism
class user < activerecord::base def get_required_app(app) required_permissions = [] app.permissions.each |p| if self.permissions.find(:first, conditions => { :permission_id => p.id } ).nil? required_permissions.push p end end required_permissions end def install_app(app) req = required_permissions app homecoming req if req.count > 0 #add user app end end
hope helps work through problem , allow me know if need additional information.
ruby-on-rails ruby ruby-on-rails-3
Comments
Post a Comment