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
Post.allor do you only want posts that have a category_id set?