Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created July 25, 2022 13:11
Show Gist options
  • Save djberg96/8c2d4571b78348ec42291e59c3a1defd to your computer and use it in GitHub Desktop.
Save djberg96/8c2d4571b78348ec42291e59c3a1defd to your computer and use it in GitHub Desktop.
# user.rb
class User < ApplicationRecord
has_many :groups_users, :class_name => 'GroupsUsers'
has_many :groups, :through => :groups_users
end
# group.rb
class Group < ApplicationRecord
has_many :groups_users, :class_name => 'GroupsUsers'
#has_many :users, :through => :groups_users
def users
User.all # requires eager loading
end
end
# groups_users.rb
class GroupsUsers < ApplicationRecord
belongs_to :group
belongs_to :user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment