多个关联的属性

Abr*_*ram 2 ruby-on-rails

所以我想做这样的事情:

@ book.chapters.words.uniq

一本书有很多章节.一章有很多单词.

如何使用活动记录完成?

或者,如何使用原始sql完成?

j-d*_*exx 5

你可以这样做

class Book
  has_many :chapters
  has_many :words, -> { uniq }, through: :chapters
end
Run Code Online (Sandbox Code Playgroud)

然后 @book.words