Ruby-on-rails – Rails has_many with alias name


In my User model I could have:

has_many :tasks

and in my Task model:

belongs_to :user

Then, supposing the foreign key 'user_id' was stored in the tasks table, I could use:


My question is, how do I declare the has_many relationship such that I can refer to a User's Tasks as:

… or …


Thanks a heap.

Best Solution

Give this a shot:

has_many :jobs, foreign_key: "user_id", class_name: "Task"

Note, that :as is used for polymorphic associations.

Related Question