2

I have the following models:

class Book < ApplicationRecord
  has_many :chapters
end

class Chapter < ApplicationRecord
  belongs_to :book
  has_many :pages
end

class Page < ApplicationRecord
  belongs_to :chapter
  has_many :paragraphs
end

class Paragrpah < ApplicationRecord
  belongs_to :page
end

Now I want to get a list of all paragraphs in a specific book. Something like:

@parapgraphs = Paragraph.pages.chapters.book.where(:title => 'My Book')

When I do this, I get:

undefined method 'chapters' for 'pages'

I'm using Rails 4.2 and Ruby 2.2

fnllc
  • 3,047
  • 4
  • 25
  • 42
  • The chapters method should be 'chapter' as in: @parapgraphs = Paragraphs.pages.chapter.book.where(:title => 'My Book') – bkunzi01 Mar 31 '16 at 21:26
  • This should do the trick `Paragraphs.includes(page: { chapter: :book }).where(books: { title: 'MyBook' })` (similar to http://stackoverflow.com/questions/18082096/rails-4-scope-to-find-parents-with-no-children/) – MrYoshiji Mar 31 '16 at 21:27

2 Answers2

6

If you want links between any of those objects to always be present and queryable, consider adding a has_many through relationship.

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

If it's more of a one time query, you could do something like this

@paragraphs = Paragraph.joins(page: { chapter: :book }).where(books: { title: 'My Book' })
Alexa Y
  • 1,854
  • 1
  • 10
  • 13
  • The where clause uses the table's name, not the relation's name (should be `where(books: { title: 'some title' })`) – MrYoshiji Mar 31 '16 at 21:29
1

Try this instead

@paragraphs = Book.find_by(title: 'My book').chapters.map{ |c| c.pages.map{ |p| p.paragraphs.to_a }.flatten }.flatten
Jacob Rastad
  • 1,153
  • 10
  • 24