仅包含活动记录的最新/最新关联记录?

cod*_*uby 8 activerecord scope ruby-on-rails-3

是否可以仅加载关联表的最新关联记录?

一个例子:

class author
  attr_accessible :first_name, :last_name, :birthday
  has_many :books
end

class book
  attr_accessible :pages, :date of publication, :title
  belongs_to :author
end
Run Code Online (Sandbox Code Playgroud)

有没有办法生成一个范围来加载作者写的最新发布的书?还是这本书的页面最多?

我知道,我可以加入或加入所有书籍.但我不知道是否可以为每位作者加载一本特定的书.

这样我就可以这样做一个查询:

Author.authors_and_their_newest_book
Run Code Online (Sandbox Code Playgroud)

这样我就能得到这些结果

first_name_author_1, last_name_author_1, birthday_author_1, pages_book_3, date_of_publication_book_3, title_book_3
first_name_author_2, last_name_author_2, birthday_author_2, pages_book_5, date_of_publication_book_5, title_book_5
first_name_author_3, last_name_author_3, birthday_author_3, pages_book_9, date_of_publication_book_9, title_book_9
Run Code Online (Sandbox Code Playgroud)

...

更新:我意识到我的真​​正问题并没有通过回答这个问题来解决.

我必须考虑第三种模式.

class author
  attr_accessible :first_name, :last_name, :birthday
  has_many :books
end

class book
  attr_accessible :pages, :date of publication, :title, _genre_id
  belongs_to :author
  belongs_to :genre
end

class genre
  attr_accessible :name
  has_many :books
end
Run Code Online (Sandbox Code Playgroud)

我真正的问题是我想用范围过滤

scope :with_latest_book_is_a_thriller
scope :with_latest_book_is_a_science_fiction
scope :with_latest_book_is_a_genre_xyz
...
Run Code Online (Sandbox Code Playgroud)

我想当我为每个作者提供最新书的范围时,我可以用genre_id的另一个范围链接它.但这导致了一个结果表,我没有"作者,以防他们的最新书是类型xyz",但"作者和他们的最新着作来自xyz类型".

例如,我有以下作者的书:

author_1: Bruce Wayne  
books:   
title: "how to fight a villain",date_of_publication: March 2010,genre: self-help,  
title: "my life as batman",date_of_publication: April 2012,genre: biography,  
title: "developing high tech equipment", date_of_publication: January 2013, genre: science  

author_2: Clark Kent  
books:  
title: "how I crossed the universe", date_of_publication: January 2009, genre: biography,  
title: "controlling a freezing breath", date_of_publication: February 2012, genre: self-help,  
title: "Clark Kent as Batman", date_of_publication: December 2013, genre: fiction  

author_3: Peter Parker  
books:  
title: "Spider-Man's life", date_of_publication: January 2010, genre: biography,  
title: "how to handle a life with a clone", date_of_publication: February 2011, genre: self-help,  
title: "Spider-Man is becoming a baker", date_of_publication: November 2013, genre: fiction  
Run Code Online (Sandbox Code Playgroud)

我想要以下结果:

scope :with_latest_book_is_a_self_help => empty result  
scope :with_latest_book_is_a_biography => empty result  
scope :with_latest_book_is_a_science => Bruce Wayne, "developing high tech equipment"  
scope :with_latest_book_is_a_fiction => Clark Kent, "Clark Kent as Batman"; Peter Parker, "Spider-Man is becoming a baker" 
Run Code Online (Sandbox Code Playgroud)

但我得到的是:

scope :with_latest_book_is_a_self_help => Bruce Wayne, "how to fight a villain"; Clark Kent, "controlling a freezing breath"; Peter Parker, "how to handle a life with a clone"
Run Code Online (Sandbox Code Playgroud)

我怎么能意识到这一点?还是不可能?

cod*_*uby 8

这是一个工作范围,lambda将参数传递给范围以选择类型id.

scope :latest_with_genre, lambda do |searched_genre_id|
  joins(:books)
    .where('books.date_of_publication = (SELECT MAX(books.date_of_publication) FROM books WHERE books.author_id = authors.id)')
    .where("books.genre_id = #{searched_genre_id}").group('author.id')
end
Run Code Online (Sandbox Code Playgroud)

这个答案Rails查询通过仅限于最近记录的关联?来自 Pan Thomakos的帮助我的范围.
这个答案来自keymone的范围传递参数帮助我传递参数