Skip to content

Instantly share code, notes, and snippets.

@minjindang
Created October 13, 2015 13:29
Show Gist options
  • Save minjindang/664462731dc94e603c46 to your computer and use it in GitHub Desktop.
Save minjindang/664462731dc94e603c46 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