Jayaram
Jayaram

Reputation: 839

Update a String Attribute in Rails

I am trying to update a string attribute in the database.

I tried using update_attribute but it isn't working. It works for integer attributes but not for String attributes.

How do i solve this ?

EDIT

code example:

@post = Post.find(params[:post_id])
@comment = @post.comments.create!(params[:comment])
@comment.update_attribute(:commenter,User.find_by_id(session[:user_id]).name)

Upvotes: 1

Views: 2966

Answers (1)

Maran
Maran

Reputation: 2736

First off, is there any reason you save the name as a string in the database? Normally you would go through the association to get the name.

@comment.user.name

I would really suggest you add a user_id to the comments table and then use:

@comment.user = User.find_by_id(session[:user_id]) 

or

@comment.update_attribute(:user_id, session[:user_id])

to update the commenter.

Upvotes: 4

Related Questions