# Recent Posts Design Question (class interaction)

**URL:** <https://www.ruby-forum.com/t/recent-posts-design-question-class-interaction/136493>\
**Category:** Rails\
**Created:** [May 2, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/recent-posts-design-question-class-interaction/136493 "2008-05-02T18:05:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [May 2, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/recent-posts-design-question-class-interaction/136493/1 "2008-05-02T18:05:43Z")

</div>

Hi all,

Well, I’ve been slowly starting to use Ruby on Rails over the past  
year but obviously haven’t learned very much yet.

I’ve been developing a simple CMS website tool in Ruby (I know, there  
are already a lot of CMS tools out there…) but it’s something I’ve  
wanted to complete for a while but haven’t got around to it. Anyway,  
in this CMS program there are (obviously,) users. Each user has his/  
her own “home”. When a user logs into the CMS system they are taken  
to their “home” page. On this “home” page there are ideally going to  
be a few different lists of things like “recent entries”, “recent  
comments”, etc.

My question is not specifically how to do all of that. But my  
question is related more to the design/setup of this type of process.  
For example, at the moment I have two major parts to this CMS: “User”  
and “Page”. (a Page is like a blog post… web page… etc.) Anyway,  
I don’t understand in Ruby on Rails how to communicate between two  
classes. For example, I would think my “recent entries” list I  
mentioned earlier would go into the Page class… would that go into  
the Page.rb model? But I don’t know how to reach that method/function  
from the User class.

I’ve been searching for an answer to anything similar to this on RoR  
Talk… but I haven’t found what I’m looking for yet. I’ve read on a  
lot of posts that it’s good to put functions in Application.rb if you  
need them to be accessed by multiple classes. I’m trying to keep each  
class as modularized as possible, too. So many questions… I just  
don’t understand how best to design something where you need one class  
to be able to access another classes’ function and display it later.  
Is this something I need to do with a partial?

Thanks,

andy

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [May 2, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/recent-posts-design-question-class-interaction/136493/2 "2008-05-02T18:12:48Z")

</div>

On May 2, 5:04 pm, summea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My question is not specifically how to do all of that. But my  
> question is related more to the design/setup of this type of process.  
> For example, at the moment I have two major parts to this CMS: “User”  
> and “Page”. (a Page is like a blog post… web page… etc.) Anyway,  
> I don’t understand in Ruby on Rails how to communicate between two  
> classes. For example, I would think my “recent entries” list I  
> mentioned earlier would go into the Page class… would that go into  
> the Page.rb model? But I don’t know how to reach that method/function  
> from the User class.  
> The Page model sounds like a reasonable enough place for this. Define  
> it as a class method (since clearly the list of recent posts isn’t  
> something which is relevant to one particular post, and then call  
> Page.find\_recent wherever you want.

Assuming your user has a has\_many :pages association, then you’ll get  
some\_user.pages.find\_recent for free (and would return the recent  
pages created by that user)

> I’ve been searching for an answer to anything similar to this on RoR  
> Talk… but I haven’t found what I’m looking for yet. I’ve read on a  
> lot of posts that it’s good to put functions in Application.rb if you  
> need them to be accessed by multiple classes.

That’s just wrong. All your controllers inherit from application.rb so  
it can be a good place for stuff shared across all controllers (eg a  
before\_filter that checks if the user is logged in).

I think you might benefit from stepping away from Rails for a minute  
and just thing/read about ruby as it doesn’t sound like you understand  
the object oriented side of things

Fred

---

<div class="post-metadata">

**Author:** ![summea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@summea](https://www.ruby-forum.com/u/summea)\
**Post date:** [May 2, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/recent-posts-design-question-class-interaction/136493/3 "2008-05-02T18:20:29Z")

</div>

On Fri, May 2, 2008 at 12:04 PM, summea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> her own “home”. When a user logs into the CMS system they are taken  
> mentioned earlier would go into the Page class… would that go into  
> Is this something I need to do with a partial?
> 
> Thanks,
> 
> andy

One way to do it is to make sure in your User model you have this:

has\_many :posts do  
def recent\_entries  
find(:all, :order =\> ‘created\_at desc’, :limit =\> 5)  
end  
end

I’m doing this off the top of my head, so may have got the syntax  
wrong. This allows you to do:

User.find(:first).posts.recent\_entries

to get the first user’s 5 most recent entries (assuming you have  
included a “created\_at” field in your posts table).

HTH,

Jamey
