views:

14

answers:

1

Hello everyone!

Newb in Rails

i Have this problem that i cannot figure out. I've followed the sample blog dimostration form the ruby doc but now i have a problem.

Let's say that in the app index page for each post i also want to show the first comment of that post.

sure i need to cycle all the post to get the post id but how can i get the first comment of that post?

how can i manage the homeController and the view ?

thanks since now!

+1  A: 

It's a bit hard to follow the question you're asking and the detailed syntax might vary, but you're going to want something like

first_comment = Comment.find_by_post_id(@post.id, :order => "created_at ASC")

(find_by_x defaults to the first of x based on your ordering, so this will only return one element.

I hope that helps.

scottru
thanks a lot mate! the real thing is we can say that i've got galleries that has_many photos and in the index page of my app i want to show the first photo for each gallery.i was using the post/comment exemple i dont' know why :Dso in my home_controller in the index def i should put something like :def [email protected] first_photo = Photo.find_by_project_id(@project.id, :order => "created_at ASC") end
Asso