I have these models and associations. I want to reach roleable trough privilege model doesnt matter what roleable_type is(Dj or Photographer)? Use join model because privilege model will have other attributes. It is possible something like this:
class User
has_one :privilege, dependent: :destroy
has_one :roleable, through: :privilege
end
class Dj < ActiveRecord::Base
has_one :privilege
has_one :user, through: :privilege, as: :roleable
end
class Photographer < ActiveRecord::Base
has_one :privilege
has_one :user, through: :privilege, as: :roleable
end
class Privilege < ActiveRecord::Base
belongs_to :user
belongs_to :roleable, polymorphic: true
end
If i add source_type: 'Dj' to has_many :through return only with roleable_type 'Dj'. I want to do this bellow:
u = User.first
u.roleable #return privilage roleable( doesnt matter Dj or Photograher)
class Photographer < Roleable
. Then you still have to make aroleable.rb
file. And you could put all your shared methods in this as well. If this is what you want, you wouldn't need tables for photographer or DJ. just a table that represents Roleable. – Tripu.roleable
could potentially return a Photgrapher object? What does it return now? – Trip