Brian V. Hughes wrote:
r00by n00by wrote:
author has_many topics
topic belongs_to author
topic has_one post
post belongs_to topic
Maybe you don’t understand why a topic has just one post, but it
supposed to be like that. It don’t think it matters. The problem is the
same in both cases.
You’re right, I don’t understand why a topic has only one post, since it
like posts are the things that authors create, it looks like the model
make more sense if author had many posts and posts had one topic.
However, I’m not sure that really matters that much, for getting the
want. Given that there’s a 1-to-1 map between topics and posts, getting
posts (with their topic) yields the same data as getting all topics
post). The difference is one is a list of post objects the other is a
The reason this is a good thing, for you, is it will allow you to do a
topics = Topic.find(:all, :include => [:post, :author], :order =>
Now, what you’ll have is a group of topics, but if you iterate over
you can simply reference t.post and t.author. And t.author will be nil
there is no author for the topic.
Does that give you something you can work with?
That’s an interesting solution. It could be even better than my original
plan (I mean Post.find).
However, what if there is another table to include?
Here is an example: we have 4 tables - songs, albums, artist and
websites. An album belongs to a single artist which may have a website.
Each album has songs (which are all preformed by the same artist, so
there is no artist id in the songs table, only in albums).
If we want to find all the songs with the artists, we do:
albums = Album.find(:all, :include => [:song, :artist] )
but what if we want to get the wesite as well? I didn’t check, but I’m
albums = Album.find(:all, :include => [:song, :artist, :website] )