Skip to content

Instantly share code, notes, and snippets.

@midu
Last active December 16, 2015 21:29
Show Gist options
  • Select an option

  • Save midu/5499877 to your computer and use it in GitHub Desktop.

Select an option

Save midu/5499877 to your computer and use it in GitHub Desktop.
# UserVideo
# reference :user
# reference :video
# boolean :favorite
class UserVideo < ActiveRecord::Base
belongs_to :user
belongs_to :video
end
class Video < ActiveRecord::Base
end
class User < ActiveRecord::Base
has_many :user_videos
has_many :videos, :through => :user_videos
end
# recupere une video
video = Video.last
# cree une association
user.user_videos.create(video: video, favorite: true) # ou false
# choppe que les videos favorites
user.videos.where(user_videos: { favorite: true })
# change une video en favorite
user.user_videos.where(video: video).update(favorite: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment