Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created October 7, 2008 21:56
Show Gist options
  • Save mikehale/15396 to your computer and use it in GitHub Desktop.
Save mikehale/15396 to your computer and use it in GitHub Desktop.
class ContestController < ApplicationController
session :off
no_login_required
skip_before_filter :verify_authenticity_token
def index
render :text => "helloworld!"
end
def create
c = Contestant.new(:email => params[:contestant_email])
c.invite_friends(params[:friend_emails])
c.save!
render :text => c.inspect
end
end
class Contestant < ActiveRecord::Base
has_many :invitations, :foreign_key => 'inviter_id'
has_many :invites, :class_name => "Invitation", :foreign_key => 'invitee_id'
has_many :inviters, :through => :invites
has_many :invitees, :through => :invitations
def entries
self.invitations.size + 1
end
def invite_friends(friend_emails)
friend_emails.each{|email|
self.invitations << Invitation.create!(:email => email)
} if friend_emails
end
end
class Invitation < ActiveRecord::Base
belongs_to :inviter, :class_name => 'Contestant'
belongs_to :invitee, :class_name => 'Contestant'
def email=(email)
unless [:invitee].nil?
self.create_invitee!(:email => email)
else
[:invitee][:email] = email
end
end
def email
[:invitee][:email]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment