Radulescu Petru Radulescu Petru - 1 year ago 87
Ruby Question

Ruby on Rails blog application posts display

I was wondering if there's a way to display both my last post created and also the latest three post created in the same page.

So this is how it looks now.what I want is to keep the left column like it is,but on the right column I want my last 3 posts to be displayed:

In my welcome_controller.rb I have this index method defined

@post =Post.limit(1).order("created_at desc")
that limits the posts to one but if I change it to 3 it applies to both left and right column and I get this really ugly thing]:

Answer Source

You could define your data in the action, like

@created_post     = Post.limit(1).order("created_at desc")
@last_three_posts = Post.limit(3).order("created_at desc")

and change your view accordingly but the created post will always be in the last three so you could use just @last_three_posts

@last_three_posts = Post.limit(3).order("created_at desc")
@created_post     = @last_three_posts.first
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download