Skip to content

Instantly share code, notes, and snippets.

@henkm
Last active December 24, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henkm/6717960 to your computer and use it in GitHub Desktop.
Save henkm/6717960 to your computer and use it in GitHub Desktop.
# new model:
# /app/models/letsgo_user.rb
belongs_to :user
belongs_to :letsgo
# changes these models:
# /app/models/user.rb
has_many :letsgo_users
has_many :letsgos, through: :letsgo_users
# /app/models/letsgo.rb
has_many :letsgo_users
has_many :users, through: :letsgo_users
def link_to_user(user_object)
letsgo_users.where(user_id: user_object.id).first_or_create
end
# /app/controllers/letsgo_controller.rb
#create action
@letsgo.save
@letsgo.link_to_user(current_user)
#repost action
@letsgo = Letsgo.find(params[:id])
@letsgo.link_to_user(current_user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment