Skip to content

Instantly share code, notes, and snippets.

@littlemove
Created May 11, 2010 08:55
Show Gist options
  • Save littlemove/397083 to your computer and use it in GitHub Desktop.
Save littlemove/397083 to your computer and use it in GitHub Desktop.
class Event
has_many :attendings
has_many :attendees, :through => :attendings, :source => :user do |id|
# Esto es un poco oscuro pero se explica rápido: redefinimos el operador << para que
# al insertar un usuario en un evento, si previamente ya se encontraba apuntado al mismo
# se borre.
def <<(user)
attending = Attending.find_by_user_id_and_event_id(user.id, self.proxy_owner.id)
attending.blank? ? Attending.create(:user_id => user.id, :event_id => self.proxy_owner.id) : attending.destroy
end
end
end
# En el controlador:
class EventsController < ApplicationController
def attend
@event = Event.find(params[:id])
@event.attendees << @current_user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment