Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7758359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:20:23+00:00 2026-06-01T13:20:23+00:00

I have 2 models images and categories . The relations are: – a image

  • 0

I have 2 models images and categories.

The relations are:

– a image belongs to one category.

– a category has many images.

On the index.html.erb view from categories object, I want paginate 20 images per page for all categories.

I mean, I want paginate all images from all categories with 20 images per page.

in my index action on categories_controller.rb method I have:

def index
    @categories = Category.all
    @categories.each do |category| 
     @images = Kaminari.paginate_array(category.images).page(1).per(1)
    end
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @categories }
    end
   end

on index.html.erb I have:

<% @images.each do |image|%>

code for each image here

<% end %>  
<% paginate @images %>

But this for me it does not works. I can not see anything image showed.

how I can implement this functionality? and fix this problem? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:20:24+00:00Added an answer on June 1, 2026 at 1:20 pm
    @categories.each do |category| 
      @images = Kaminari.paginate_array(category.images).page(1).per(1)
    end
    

    In this loop, you’re overriding @images each time, so obviously, you wont get what you expect 😉

    I think that what you wanna do is:

    @images = Image.where("category_id IS NOT NULL").page(params[:page]).per(20)
    

    The .where("category_id IS NOT NULL") is not necessary if images have to belong to a category.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two models, each with an image. One has a foreign key
I have 2 models product and image. Product has an Has-many relationship with Image.
I have Four models. Product Category Categorization Images Product.rb has_many :images has_many :categorizations has_many
I have an Images model that has imageID = models.IntegerField(primary_key=True, editable=False) Then I'm doing:
I have two models: 'gallery' and 'image'. When I try to fetch images from
I have two classes, Portfolio, and PortfolioImage. class PortfolioImage(models.Model): portfolio = models.ForeignKey('Portfolio', related_name='images') ...
I have two models, Room and Image . Image is a generic model that
I have two models, Image and Tag . Each Image object can have more
I'm using ASP.NET MVC and have a model which has an image (byte array)
Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.