Reputation: 1621
I have blog post that I want to put on 2 different show views. I want the 4 most recent post to show but all of my post are 1 of 2 different :post_types
(manager or user). So i want 1 view to show the 4 most recent manager post and the other view to show the 4 most recent user post. Where should I put this logic, the controller or somewhere in the model and how do I make a method to get the 4 most recent post of each type?
Currently i have this in the controller
def index
@blog1 = Post.order(:date => :desc).first
@blog2 = Post.order(:date => :desc).offset(1).first
@blog3 = Post.order(:date => :desc).offset(2).first
@blog4 = Post.order(:date => :desc).offset(3).first
end
But it doesn't separate the post by type
Upvotes: 0
Views: 26
Reputation: 824
This will do the job:
def index
@manager_posts = Post.where(post_type: 'Manager').order('date DESC').limit(4)
@user_posts = Post.where(post_type: 'User').order('date DESC').limit(4)
end
Then in your view
<% @manager_posts.each do |manager_post| %>
<%= manager_post.content %>
<% end %>
<% @user_posts.each do |user_post| %>
<%= user_post.content %>
<% end %>
Upvotes: 1
Reputation: 1621
This seemed to get it
@blog1 = Post.where(:post_type == 'Manager' ).order(:date => :desc).first
Upvotes: 0