rails: best way to store comments in mysql
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 co开发者_Python百科mments 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.
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.
In Rails, all models automatically get a created_at
field. You can just order by that.
精彩评论