Johshi Johshi - 4 months ago 22
Ruby Question

Alias for relationship

I have this:

class User < ActiveRecord::Base
has_many :jobs


How can I rename ":jobs" to something else without renaming the source class? I just want to be to refer to them as:

class User < ActiveRecord::Base
has_many :obligations


and that's it. I've tried:

has_many :obligations, source: :user


but it didn't work.

Answer

You are doing wrong. Instead of adding source add class like this

class User < ActiveRecord::Base
  has_many :obligations,   class_name: 'Job'
end

I hope this will solve your problem.

Comments