Created
May 11, 2010 08:55
-
-
Save littlemove/397083 to your computer and use it in GitHub Desktop.
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 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