Recent Posts Design Question (class interaction)

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

On May 2, 5:04 pm, summea [email protected] 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

On Fri, May 2, 2008 at 12:04 PM, summea [email protected] 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