1

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)
8
  • 1
    You're doing something wrong. This answer is as specific as your question. Commented Sep 30, 2010 at 0:40
  • Could you post a code example? Commented Sep 30, 2010 at 0:40
  • added the code to the original post... Commented Sep 30, 2010 at 0:46
  • What is the error message, if any? What value is returned in the find statement? Could name be nil? Commented Sep 30, 2010 at 0:49
  • yea name is nil. Its not setting it to the userid from the session. but if i use the same(User.find_by_id(session[:user_id]).name) on a view page, I get the username. Commented Sep 30, 2010 at 0:53

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.