【发布时间】:2016-03-31 21:16:55
【问题描述】:
我有以下型号:
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
现在我想获取特定书中所有段落的列表。比如:
@parapgraphs = Paragraph.pages.chapters.book.where(:title => 'My Book')
当我这样做时,我得到:
undefined method 'chapters' for 'pages'
我正在使用 Rails 4.2 和 Ruby 2.2
【问题讨论】:
-
章节方法应该是“章节”,如:@parapgraphs = Paragraphs.pages.chapter.book.where(:title => 'My Book')
-
这应该可以解决问题
Paragraphs.includes(page: { chapter: :book }).where(books: { title: 'MyBook' })(类似于stackoverflow.com/questions/18082096/…)
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord where-clause