activerecord - Rails multiple has many relationships on the same model, one with an alias and polymorphic -


i have 2 models, user model , task model. prior setting new relationship, there simple has_many , belongs_to relationship, ex:

class user < activerecord::base   has_many :tasks end  class task < activerecord::base   belongs_to :user end 

this relationship in application represents user created task record, example when user creates 1 sets current user's id equal tasks.user_id.

i want relationship (the same exact type) "assigned" feature. means relationship above represents creator of task, while assigned_id represents assigned to.

however, assigned_id can user, vendor, or carrier. hence polymorphic relationship. i've set on task model , has assignable_id , assignable_type column on in database.

i added task model: belongs_to :assignable, :polymorphic => true.

what line can add make association work in user model. there has_many :tasks different purpose. how can done?

try below:

has_many :assigned_tasks, class_name: 'task', as: :assignable 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -