Created
July 3, 2013 19:01
-
-
Save denishaskin/5921734 to your computer and use it in GitHub Desktop.
how best to make associations in controller?
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
= simple_form_for(@trip) do |f| | |
= f.input :from_place, :collection => @trip.owner.places | |
= f.input :to_place, :collection => @trip.owner.places | |
-# ...etc... |
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 Trip < ActiveRecord::Base | |
belongs_to :from_place, foreign_key: 'from_place_id', class_name: Place | |
belongs_to :to_place, foreign_key: 'to_place_id', class_name: Place | |
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 TripsController < ApplicationController | |
def create | |
# what's the best way to make the association here? | |
# The form sends up IDs, so can either | |
# (1) assign IDs directly w/ mass assignment, or | |
# (2) look up the associated objects and add them to the hash (e.g. params[:trip][:from_place] = Place.find(x)) | |
@trip = Trip.new(params[:trip]) | |
respond_to do |format| | |
# ...etc... | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment