Skip to content

Instantly share code, notes, and snippets.

@agile
Last active August 29, 2015 13:56
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 agile/9137372 to your computer and use it in GitHub Desktop.
Save agile/9137372 to your computer and use it in GitHub Desktop.
class Parent < ActiveRecord::Base
# assume there's a varchar field on the table called "name"
def name
puts "Holla from tha Parent!"
super
read_attribute(:name) || "default value!"
end
end
class Child < Parent
def name
puts "Holla from tha Child!"
super
end
end
irb> c = Child.find(123)
Child Load (0.5ms) SELECT "parents".* FROM "parents" WHERE "parents"."type" IN ('Child') AND "parents"."id" = $1 LIMIT 1 [["id", 123]]
=> #<Child id: 123, type: "Child", name: nil>
irb> c.name
Holla from tha Child!
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment