Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a conditional association in model?

I have setup a role based access controll system with the following models:

  • Role (as STI),
    • UserRole (global roles)
    • ProjectRole (project specific roles)
  • Assignment (Polymorphic with different resources)
  • User
  • Project (as one resource type for assignments)

Users are only allowed to be responsible for a project if they have a specific UserRole. This Userrole is name "responsible for projects" and has ID 2.

In User model there are two has_many associations :responsible_assignments and responsible_projects. This associations are only valid if the user has the UserRole "responsible for projects" with ID 2.

Is it possible to create a conditional association in user model for responsible_* association and is this a common way to setup this kind of relations?

What is the best practise to solve this kind of problems?

class Role < ActiveRecord::Base
  has_many :assignments
  has_many :users, :through => :assignments

class UserRole < Role

class ProjectRole < Role

class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
  belongs_to :resource, :polymorphic => true

class User < ActiveRecord::Base
  has_many :assignments
  has_many :roles, :through => :assignments, 
                   :class_name => "UserRole"
  has_many :responsible_assignments, :class_name => "Assignment",
                                     :conditions => { :role_id => 4 }     // specific project role
  has_many :responsible_projects, :through => :responsible_assignments, 
                                 :source => :resource, 
                                 :source_type => 'Project',
                                 :conditions => { :status => 1 }          // project is active
  ...

class Project < ActiveRecord
  ...
like image 331
tonymarschall Avatar asked Mar 04 '12 15:03

tonymarschall


People also ask

What is the difference between Has_one and Belongs_to?

They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.

What is a polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.

What is dependent destroy in Rails?

what is dependent :destroy. Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.


1 Answers

In case anyone finds this later - this feature is now actually available in rails 4:

http://guides.rubyonrails.org/association_basics.html

Syntax is:

has_many :orders, -> { where processed: true }
like image 195
Mikey Hogarth Avatar answered Sep 20 '22 18:09

Mikey Hogarth