Skip to content

Instantly share code, notes, and snippets.

@denishaskin
Created July 3, 2013 19:01
Show Gist options
  • Save denishaskin/5921734 to your computer and use it in GitHub Desktop.
Save denishaskin/5921734 to your computer and use it in GitHub Desktop.
how best to make associations in controller?
= simple_form_for(@trip) do |f|
= f.input :from_place, :collection => @trip.owner.places
= f.input :to_place, :collection => @trip.owner.places
-# ...etc...
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
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