views:

8

answers:

0

The way this program needs to work is like this: A person can view an event and then choose to either register directly for an event or for a team (which is registered for an event).

class Event 
  has_many_polymorphs :registrants, :from => [:users, :teams], :through => :registrations
end
class Team
  belongs_to :registrant, :polymorphic => true
end
class User
end
class Registration
  belongs_to :event
  belong_to :registrant, :polymorphic => true
end

The way it's currently structured, when you choose how you want to register, it passes either an event_id, or team_id parameter. From there, you fill out a form_for :user and sign up. My question is though how I am supposed to go about creating the Join table to actually link the User & Event (or User & Team). I've tried this:

http://0.0.0.0:3000/user/new?event_id=1

def new
  @title = "Register"
  @team = Team.find_by_id(params[:team_id]) unless not params[:team_id] #unless not keeps us from accessing nil object
  @event = Event.find_by_id(params[:event_id]) unless not params[:event_id]
  @heading = "Register"

  if request.post? and params[:user]
    @user = User.new(params[:user])
    if @user.save
      @event.users << @user
      flash[:notice] = "Successfully registered"
      redirect_to :action => "success", :controller => "user", :user_id => @user
    end
  end
end

But this obviously doesn't work because the @event isn't available after the post. How do I go about creating the Registration for the user after a successful save? I thought it was supposed to be easy to create the Join table, but I'm apparently making it harder than it needs to be. Essentially I want to be able to call @event.users, @event.teams, @user.events, @user.teams, @team.users, @team.events, etc...

I've spent almost my entire day trying to wrap my head around this, but I'm apparently just not making the connection somewhere. Any help would be appreciated. You guys rock