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
3
  • why dont you just query for all posts via Post.all or do you only want posts that have a category_id set? Commented Jun 5, 2012 at 19:51
  • If I'm not mistaken you simply want to group your posts by category, see Rails Guides Commented Jun 5, 2012 at 19:53
  • I forgot to mention that I want to retrieve posts that have a category_id set! Sorry. Commented Jun 5, 2012 at 19:54

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't seem to work in my case. I just want to loop each category and use my retrieval as I tested it and it works.
Thank you for your quick answer Anil! You are right I checked my console and I was overwriting @posts, but I followed your advise and the '<<' is giving me an error: "undefined method `<<' for nil:NilClass". Are you sure those are valid brackets?
@DanieGarzon Give it another try.
Awesome! I am getting closer. In the console I am getting the desired results for @posts but because they are now grouped into different arrays,depending on the category, I am getting errors in my view: "undefined method `title'" although each post have their own title. Can you please check my original post? I did an edit to show my view.
I have no words to thank you! It worked, actually I had to add += to the stuffer. Update your answer please, and I already gave you the up! Thank you once again..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.