Created
October 25, 2011 10:42
-
-
Save mraaroncruz/1312246 to your computer and use it in GitHub Desktop.
ActiveRecord with habtm and owner
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 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