Skip to content

Instantly share code, notes, and snippets.

@mraaroncruz
Created October 25, 2011 10:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mraaroncruz/1312246 to your computer and use it in GitHub Desktop.
Save mraaroncruz/1312246 to your computer and use it in GitHub Desktop.
ActiveRecord with habtm and owner
class Playlist < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :owner, class_name: "User", inverse_of: :owned_playlist
def editable_by?(user)
self.owner == user
end
end
class User < ActiveRecord::Base
has_and_belongs_to_many :playlists
has_many :owned_playlists, inverse_of: :owner
end
# Add an owner_id to your playlists table and everything should be dandy
# you will need an playlists_users table that consists of just user_id and playlist_id for the habtm relationship
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment