Topic: make a page that contains stuff from more than one model/controller?

I'm new to ruby and rails.
I've gone through different tutorials on using the rails scaffolding.
What I don't understand is how you make a page that has different sections that are each built from a different model/controller.  Maybe the central section of the page provides the interface for a forum.  And there is a sidebar that, say, shows the latest news topics for the site.  Or another section that shows a random famous quote.  Or whatever.  The idea is how to have sub-sections of the page that are driven by their own data?

Is there a way to have the sidebar call into a different controller for its rendering?  Or how is this done?

Re: make a page that contains stuff from more than one model/controller?

I think I have answered my own question.  I discovered that you can call methods on models other than the one associated with the current controller.

For example, suppose you've got a
RecipeController and its associated Recipe model
and a
NewsItemController and its associated NewsItem model.

I can call

@newsitems = NewsItem.find_all

from an action in the RecipeController if I wish.  Then the rendering of this action can use @newsitems to fill in the sidebar.

And it looks like I could do something like this:

class ApplicationController < ActionController::Base
    before_filter :load_news
   
private
    def load_news
        @newsitems = NewsItem.find_all
    end
end

and put the sidebar in the default layout--if I want to have the sidebar in every page.

A question though: How do you retrieve, say, the 10 most recent news items (according to newsitem.created_on) rather than the whole table, which find_all returns?

Re: make a page that contains stuff from more than one model/controller?

Hello there,

I believe you shouldn't really be using find_all but rather the find :all variants.  The code you need is below.  Reference for the find method can be found at http://caboo.se/doc/classes/ActiveRecor … ml#M006448

@newsitems = NewsItem.find :all, :order => 'created_on desc', :limit => 10

Hope this helps,

Ant

Last edited by antramm (2007-07-25 09:49:19)