Skip to content

Instantly share code, notes, and snippets.

@mauricioklein
Last active November 29, 2021 01:17
Show Gist options
  • Save mauricioklein/fe98980d78f2ab6fa13d to your computer and use it in GitHub Desktop.
Save mauricioklein/fe98980d78f2ab6fa13d to your computer and use it in GitHub Desktop.
Has Many :through polymorphic problem
class Admin < ActiveRecord::Base
has_many :favorites, as: :favoritable
has_many :favorite_services, through: 'favorites', source: 'service'
end
class Favorite < ActiveRecord::Base
belongs_to :service
belongs_to :favoritable, polymorphic: true
end
class Service < ActiveRecord::Base
has_many :favorites
has_many :admins_who_favorited, through: 'favorites', class_name: 'Admin', source: 'favoritable', source_type: 'Admin'
has_many :users_who_favorited, through: 'favorites', class_name: 'User', source: 'favoritable', source_type: 'User'
end
class User < ActiveRecord::Base
has_many :favorites, as: :favoritable
has_many :favorite_services, through: 'favorites', source: 'service'
end
@mauricioklein
Copy link
Author

Gist updated with problem solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment