Using another objects methods within another object

0 views
Skip to first unread message

Gearóid O'Ceallaigh

unread,
Jun 25, 2008, 6:44:42 PM6/25/08
to Ruby on Rails: Talk
Hey,

I'm just getting used to ruby on rails and im designing a website that
has a forum and a list of events. So there are two models: forum and
event.

I've got a method for event called find_upcoming_events which is used
to list all future events. But I'd like to show the upcoming events at
the bottom of the forum page but I'm unable to do this since the
function belongs to event and not forum.

Can anyone tell me how I could do this? Thanks in advance.

Frederick Cheung

unread,
Jun 26, 2008, 3:40:45 AM6/26/08
to Ruby on Rails: Talk


On Jun 25, 11:44 pm, Gearóid O'Ceallaigh <gearoi...@gmail.com> wrote:
> Hey,
>
> I'm just getting used to ruby on rails and im designing a website that
> has a forum and a list of events. So there are two models: forum and
> event.
>
> I've got a method for event called find_upcoming_events which is used
> to list all future events. But I'd like to show the upcoming events at
> the bottom of the forum page but I'm unable to do this since the
> function belongs to event and not forum.

Sounds like find_upcoming_events should be a class method on Event,
which would mean you could call Event.find_upcoming_events (which I'd
probably shorten to Event.find_upcoming) anywhere you want. If that
sentence was gibberish, I suggest you read a little more about ruby/
object oriented programming in general.

Fred

krus...@gmail.com

unread,
Jun 26, 2008, 10:18:14 AM6/26/08
to Ruby on Rails: Talk


On 25 jun, 19:44, Gearóid O'Ceallaigh <gearoi...@gmail.com> wrote:
> Hey,
> I've got a method for event called find_upcoming_events which is used
> to list all future events. But I'd like to show the upcoming events at
> the bottom of the forum page but I'm unable to do this since the
> function belongs to event and not forum.
>

In the forum controller index action, set

@upcoming_events = Event.upcoming

The Event::upcoming method could be a proxy to find with some filters,
or just a named_scope (look for it).

Lucas.
Reply all
Reply to author
Forward
0 new messages