Skip to content

Instantly share code, notes, and snippets.

@rintaun
Last active December 17, 2015 05:29
Show Gist options
  • Save rintaun/5558281 to your computer and use it in GitHub Desktop.
Save rintaun/5558281 to your computer and use it in GitHub Desktop.
class User < Sequel::Model
one_to_many :memberships
many_to_many :teams, :join_table => :memberships
end
class Membership < Sequel::Model
many_to_one :user
many_to_one :team
end
class Team < Sequel::Model
one_to_many :memberships
many_to_many :users, :join_table => :memberships
def membership_id
# ???
end
def hash_with_membership_id
hash = values
hash[:membership_id] = membership_id
hash
end
end
User.create(:id => 1)
Team.create(:id => 1)
Membership.create(:id => 1, :user_id => 1, :team_id => 1)
User[1].teams.collect { |t| t.hash_with_membership_id } # [{:id => 1, :membership_id => 1}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment