如何限制响应中关联中包含的记录数?

时间:2013-08-29 01:22:07

标签: ruby-on-rails rails-activerecord

我以下列方式发回一个json对象:

respond_with @authors, include: :posts

我需要限制posts的数量。我尝试过:

respond_with @authors, include: :posts, limit: 10

但它不起作用。

1 个答案:

答案 0 :(得分:2)

我在这里做的是为latest_posts创建一个关系:

class Author
  has_many :latest_posts, -> { limit(10) }, class_name: Post
end

# usage

respond_with @authors, include: :latest_posts