Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Last active August 29, 2015 14:18
Show Gist options
  • Save tiagopog/56ed29bcebed00c852c7 to your computer and use it in GitHub Desktop.
Save tiagopog/56ed29bcebed00c852c7 to your computer and use it in GitHub Desktop.
Polymorphic association example with "belong_to".
class Vehicle < ActiveRecord::Base
validates :name, presence: true
end
class Car < ActiveRecord::Base
belongs_to :vehicle
accepts_nested_attributes_for :vehicle
delegate :name, to: :vehicle
end
--
> c = Car.new(doors: 4, vehicle_attributes: { name: 'Golf', brand: 'Volkswagen' })
> c.name #=> "Golf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment