0

I would like to create a page with rails to show the embedded documents of multiple documents. The problem is that the number of embedded documents per document is variable.

I have multiple posts and each post can have up to 5 images. The image is an embedded document of the post.

class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  field :text, :type => String

  belongs_to :user
  embeds_many :post_images
  ...
end

class PostImage
  include Mongoid::Document

  mount_uploader :image, PostImageUploader

  embedded_in :post

end

Now I want to create a page to show all post_images of all posts. I want to show 20 post images per page. I am using kaminari for pagination.

This code limits the posts per page to 20. But I want to limit the post_images to 20 per page:

@posts = Post.where({:user_id => @user._id, :post_images.exists => true}).desc(:created_at).page(params[:page]).per(20)

How can I do that?

roemchine
  • 11
  • 2
  • If you want 20 posts and 20 images per page then why not just limit a post to show single image? For posts more than 1 image you can add a link which will take you to posts show page where you can show all the images – Mandeep Aug 04 '14 at 13:33
  • I do not want to limit to limit the posts on 20 per page. Just the post_images should be limited to 20 per page. I just want to show all images of a user on a specific site in the profile. – roemchine Aug 04 '14 at 19:17

1 Answers1

1

I found a solution to limit the number of post_images to 20 per page:

all_post_images = Post.where({:user_id => @user._id, :post_images.exists => true}).desc(:created_at).map{|p| p.post_images}.flatten
@post_images = Kaminari.paginate_array(all_post_images).page(params[:page]).per(20)
roemchine
  • 11
  • 2