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 8035691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:22:49+00:00 2026-06-05T02:22:49+00:00

I am trying to retrieve from my database all posts and list them in

  • 0

I am trying to retrieve from my database all posts and list them in DESC order with respect to their creation date. So far I have managed to test for all posts that belong to one category but I want to display all posts no matter what category they belong to. I know I have to loop trough each category, and get the posts from each but I dont know how to. Here is my code:

EDIT:

  def index
    @institution = Institution.find(current_user.institution.id)
    @categories = Category.all
    @categories.each do |category|
      @posts = Post.where("category_id = ? and institution_id = ?", category, @institution).order("created_at DESC")
    end
    authorize! :read, @post
    respond_with(@posts)
  end

Can someone please point me in the right direction?

EDIT 2: My view (index.html.haml)

%h1 Listing posts

%table
  %tr
    %th Title
    %th Description
    %th User
    %th Category
    %th Type
    %th Class
    %th Institution

  - @posts.each do |post|
    %tr
      %td= post.title
      %td= post.description
      %td= post.user_id
      %td= post.category_id
      %td= post.institution_id
  • 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-05T02:22:51+00:00Added an answer on June 5, 2026 at 2:22 am

    You are overwriting @posts with each iteration.
    Try this:

    def index
        @institution = Institution.find(current_user.institution.id)
        @categories = Category.all
        @posts = []
        @categories.each do |category|
          tposts = Post.where("category_id = ? and institution_id = ?", category, @institution).order("created_at DESC")
          @posts += tposts if tposts
        end
        authorize! :read, @post
        respond_with(@posts)
    end
    

    To retrieve all posts with non null category_id, try this:

    def index
        @institution = Institution.find(current_user.institution.id)
        @categories = Category.all
        @posts = Post.where("category_id is not null and institution_id = ?", @institution).order("created_at DESC")
        authorize! :read, @post
        respond_with(@posts)
    end
    

    Change is not null to > 0 for integer category_id or != '' if your table contains ” instead of nulls.

    Good luck.

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

Sidebar

Related Questions

I'm trying to retrieve all the data id from a database where their tags(array)
Iam trying to retrieve data from mysql database into stylesheet.php but it is not
I'm trying to retrieve information from an Oracle database using XQuery. The table customer
I am trying to retrieve specific data from my MySQL database by passing the
I'm trying to retrieve record data from an MS-Access database and store it into
I am trying to store and retrieve photos from a MySql database. Can someone
I'm trying to generate a list dynamically from database. The results can be retrieved,
I'm trying to create a efficient query which will retrieve 'projects' from a database
I am trying to retrieve a blob from a postgres database using the jdbc
I am trying to retrieve fields from a table in a SQLite database. I've

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.