Results 1 to 4 of 4

Thread: Ruby On Rails- Getting entries from a different table

  1. #1
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default Ruby On Rails- Getting entries from a different table

    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?

    Edit: UPDATE: Here's my code:
    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>
            </li>
    <% end %>
          </ul>
    
          <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>
            </li>
    <% end %>
          </ul>

    Thanks,
    Alex
    Last edited by djr33; 12-31-2010 at 06:14 AM.
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  2. #2
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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, rder => "id desc", :limit => 5)
    @last_five_videos = Videos.find(:all, rder => "id desc", :limit => 5)

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

  3. The Following User Says Thank You to julesdagreat For This Useful Post:

    X96 Web Design (11-29-2010)

  4. #3
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That is a nice solution. To make this job much easier for you. You can use the Rails Delayed Job so that it will update regularly as you want it.

  5. #4
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Hmm. This thread is resolved, yet I can't edit my post to mark it as resolved...

    - Alex
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •