Skip to content

Instantly share code, notes, and snippets.

@tubbo
Forked from pduersteler/controller.rb
Last active August 29, 2015 14:02
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 tubbo/f6832d295dff3c83722d to your computer and use it in GitHub Desktop.
Save tubbo/f6832d295dff3c83722d to your computer and use it in GitHub Desktop.
class EnrollmentController < ApplicationController
def new
@users_assignments = []
3.times{ @users_assignments << @event.assignments.build }
end
def create
@event = Event.new params[:event]
@event.
redirect_to 'new' and return unless @event.save
render @event
end
end
<%= semantic_form_for @event do |f| %>
<!--
Usually, there would be semantic_fields_for :jobs here,
but the user choses the job in a wishlist-form.
This leads to a
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection error, saying
"Cannot modify association 'Event#assignments' because the source reflection class 'Assignment'
is associated to 'Job' via :has_many"
-->
<%= f.semantic_fields_for :assignments, @assignments do |a| %>
<%= a.input :job, as: :select, collection: @users_assignments %>
<% end %>
<% end %>
class Event < ActiveRecord::Base
has_many :assignments
has_many :jobs, through: :assignments
end
class Job < ActiveRecord::Base
has_many :assignments
has_many :events, through: :assignments
end
class Assignment
belongs_to :job
belongs_to :event
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment