views:

33

answers:

2

Hello.

Okay i have two models: posts and comments. as you can think comments has column :post_id.

My models

Comments

  • belongs_to :post

Post

  • has_many :comments

So, this is pretty simple association but i have some problems with ordering comments. at first time, when i create my comments migration file i just add column :position. This column indicate comment position in the post.

But now i think what where is more good way to do this.

so i can't make my choise:

1) uses t.column :datatime :created_at, :default => Time.now()

2) or use timestamps? this is undiscovered for me, please tell me about your exp.

A: 

In Rails, all models automatically get a created_at field. You can just order by that.

Yar
i know. but i don't need updated_at column because users can't update weir comments :D
rbdev
+3  A: 

yar is right. No need to have a position column, unless you'll want to reorder your comments, which I doubt you will :) Just do

has_many :comments, :order => "created_at DESC"

and every time you use @post.comments they will be sorted by date.

neutrino
+1 for saying I'm right :)
Yar