View Full Version : Resolved Ruby On Rails- Getting entries from a different table

X96 Web Design
11-26-2010, 08:48 PM
Hi There,

First post in a while, so here goes...

I've been doing a lot of Ruby on Rails development lately, but can't figure out one thing. I have two different "scaffolds", one called 'games' and another called 'videos'.

I want to have the latest 5 from each displayed on the homepage, but can't figure out how to tell Rails to do so.

I'm assuming there's a statement I can put either in or before the 'each' block?

UPDATE: Here's my code:

<ul class="gallery latest">
<% @games.each do |game| %>
<li class="game">
<%= link_to image_tag("games/" + game.slug + ".jpg", :border=>0), game %>
<div class="name"><%= game.title %></div>
<% end %>

<ul class="gallery latest">
<% @videos.each do |video| %>
<li class="video">
<%= link_to image_tag("videos/" + video.slug + ".jpg", :border=>0), video %>
<div class="name"><%= video.title %></div>
<% end %>


11-29-2010, 09:32 PM
You need to do this in the controller, not the view. Really, you shouldn't have any logic at all in your Views, it should just be displaying stuff to the screen not figuring out anything about the data you want to present.

Haven't used RoR for a few months but you want something like this:

@last_five_games = Games.find(:all, :order => "id desc", :limit => 5)
@last_five_videos = Videos.find(:all, :order => "id desc", :limit => 5)

Once you have these two variables defined in the controller, you can simply print them out in your View.

12-30-2010, 11:25 PM
That is a nice solution. To make this job much easier for you. You can use the Rails Delayed Job (http://www.simpleworker.com/) so that it will update regularly as you want it.

X96 Web Design
12-31-2010, 01:23 AM
Hmm. This thread is resolved, yet I can't edit my post to mark it as resolved...

- Alex