question about making comment in blog system

The create() method takes in a hash containing keys names the same as fields in the respresentative table. In this case, you’ll probably be passing in:

params = { :post => { :id => [postid] }, { :comment => { :body => ‘some body text’ } }

Thus, you can just modify the poster form to include a comment_poster_id field which will automatically be added into the hash for you.

However if you need to add such values in the Controller, you can do this:

def add_comment
comm =[:comment])
comm.poster_id = poster_id
comm.commented_time = # or whatever

Post.find(params[:id]).comments << comm # I think this saves for you but I can’t remember.


There are many different ways of manipulating ActiveRecord objects. If in doubt, just seperate out into seperate objects and do what you need.

Hope that helps.


For comm.commented_time, you can use ActiveRecord’s “magic” created_at column, which will default to the time the Record is created without you having to do anything.

Just call the column “created_at” and make sure it can handle a timestamp (string works at worst.)