Skip to content

Instantly share code, notes, and snippets.

@zackexplosion
Forked from minjindang/請問要怎麼實作Model?
Last active October 14, 2015 02:19
Show Gist options
  • Save zackexplosion/89b56e68729c4c252d7c to your computer and use it in GitHub Desktop.
Save zackexplosion/89b56e68729c4c252d7c to your computer and use it in GitHub Desktop.
Ruby on Rails
# Try to create a "Person" model,that we can use the "children" method below.
tom = Person.create(name: "Tom")
may = Person.create(name: "May", parent: tom)
syd = Person.create(name: "Syd", parent: tom)
tom.children.map(&:name)
# => ["Syd", "May"]
# Furthermore,can you design "grandchildren" method that we can use it like this?
wen = Person.create(name: "Wen", parent: syd)
jon = Person.create(name: "Jon", parent: may)
tom.grandchildren.map(&:name)
# ["Wen", "Jon"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment