Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何限制响应中关联中包含的记录数?_Ruby On Rails_Rails Activerecord - Fatal编程技术网

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

Ruby on rails 如何限制响应中关联中包含的记录数?,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我以以下方式发回一个json对象: respond_with @authors, include: :posts 我需要限制帖子的数量。我试过: respond_with @authors, include: :posts, limit: 10 但是它不起作用。我在这里要做的是为最新发布创建一个关系: class Author has_many :latest_posts, -> { limit(10) }, class_name: Post end # usage respo

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

respond_with @authors, include: :posts
我需要限制
帖子的数量
。我试过:

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

但是它不起作用。

我在这里要做的是为
最新发布创建一个关系:

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

# usage

respond_with @authors, include: :latest_posts