Skip to content

Instantly share code, notes, and snippets.

@Tigraine
Created June 25, 2012 11:39
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 Tigraine/2988112 to your computer and use it in GitHub Desktop.
Save Tigraine/2988112 to your computer and use it in GitHub Desktop.
Persons Demo
@person = Person.first # a Person
link_to @person.name, @person # => /persons/1
@student = Student.first # a Student
link_to @student.name, @student # => /students/2
# this allows stuff like this:
@persons = Person.all # Person.all also includes Students
# @persons will look like this: [Person, Student, ..]
@persons.each do |p|
link_to p.name, p
end
# => /persons/1, /students/2
class AddTypeColumnToPersons < ActiveRecord::Migration
def change
add_column :person, :type, :string
end
end
class Person < ActiveRecord::Base
end
class Student < Person
end
@persons = Person.all
@persons.each do |p|
link_to @p.name, edit_polymorphic_path(p)
end
# => /persons/1/edit, /students/2/edit
@persons = Person.all
@persons.each do |p|
link_to p.name, edit_person_path(p)
# => /persons/1/edit, /persons/2/edit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment