-
-
Save tubbo/f6832d295dff3c83722d 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 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 |
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
<%= 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 %> |
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 < 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