Last active
November 29, 2021 01:17
-
-
Save mauricioklein/fe98980d78f2ab6fa13d to your computer and use it in GitHub Desktop.
Has Many :through polymorphic problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin < ActiveRecord::Base | |
has_many :favorites, as: :favoritable | |
has_many :favorite_services, through: 'favorites', source: 'service' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Favorite < ActiveRecord::Base | |
belongs_to :service | |
belongs_to :favoritable, polymorphic: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :favorites, as: :favoritable | |
has_many :favorite_services, through: 'favorites', source: 'service' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gist updated with problem solution.