Search code examples
ruby-on-railsdatabaseruby-on-rails-4consolerecords

Viewing Nested Records in Rails Console


I am trying to view nested records from the rails console.

I have subarticles nested into articles. In other words, subarticles belongs to article, and articles has many subarticles.

I have tried something like Article.subarticle.all from the console but this doesn't work.

What is the best way to go about this?


Solution

  • If you want to view all subarticles to one particular Article, you just need to assign it to a variable:

    a = Article.find(1) - this will assign Article with id = 1

    And then call: a.subarticles - this will show all subarticles associated with your Article