acts_as_commentable can I create without updating?

60 Views Asked by At

The current controller code I'm using to create a comment on a Post object is this:

    commentable = @post
    comment = commentable.comments.create
    # comment.title = params[:title] #Title not needed
    comment.comment = params[:comment][:comment]
    comment.user = current_user

    gon.post_id = @post.id #for javascript

    if comment.save
    ....

This is also how it's recommended on the docs.

Is there a way to do this by passing the params into the .create function, like

(user = current_user ....)

This avoid making 2 calls to the db on a create, thus upping performance.

Additionally, if you're using the public_activity gem it makes things easier too. because it adds a Created and Updated activity on my database every time I write a comment.

Thnaks

2

There are 2 best solutions below

3
Milind On

more simpler approach to include commentable model in your rails app..Railscasts for adding commentable

0
David Sigley On

Oh, it was actually really simple.

 comment = commentable.comments.create(:comment => params[:comment], :user => current_user)

Add :title if you're using that feature.

I don't understand why this isn't the solution provided in the docs? I should probably modify it to use

create(comment_params)