# Undefined method user\_id

**URL:** <https://www.ruby-forum.com/t/undefined-method-user-id/160979>\
**Category:** Rails\
**Created:** [February 21, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/undefined-method-user-id/160979 "2009-02-21T18:46:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenle545](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/278dde/32.png) [@kenle545](https://www.ruby-forum.com/u/kenle545)\
**Post date:** [February 21, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/undefined-method-user-id/160979/1 "2009-02-21T18:46:05Z")

</div>

Basically, Im trying to add comments to photos that other users have  
uploaded. And Ive encountered an error:

NoMethodError in CommentsController#create  
undefined method `user\_id=’ for #Comment:0x4768cd0

C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/base.rb:1857:in  
`method_missing' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1672:in`send’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/base.rb:1672:in  
`attributes=' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1671:in`each’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/base.rb:1671:in  
`attributes=' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1505:in`initialize\_without\_callbacks’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/callbacks.rb:225:in  
`initialize' #{RAILS_ROOT}/app/controllers/comments_controller.rb:8:in`new’  
#{RAILS\_ROOT}/app/controllers/comments\_controller.rb:8:in `create’

This is my comments controller:

class CommentsController \< ApplicationController  
before\_filter :login\_required

def create  
@photo = Photo.find\_by\_user\_id\_and\_id(params[:user\_id],  
params[:photo\_id])

```
@comment = Comment.new(:user_id => @logged_in_user.id,
                                        :body =>

```

params[:comment][:body])  
if @photo.comments \<\< @comment  
flash[:notice] = ‘Comment was successfully created.’  
redirect\_to photos\_path(:user\_id =\> @photo.user,  
:photo\_id =\> @photo)  
else  
render :controller =\> ‘photos’, :action =\> ‘show’,  
:user\_id =\> @photo.user, :photo\_id =\> @photo  
end  
end

def destroy  
@photo = Photo.find\_by\_user\_id\_and\_id(@logged\_in\_user.id,  
params[:photo\_id],  
:include =\>  
:user)  
@comment = @photo.comments.find(params[:id])  
@comment.destroy

```
redirect_to entry_path(:user_id => @photo.user.id,
                                :id => @photo.id)

```

end  
end

It said that the user\_id is undefined when creating a new comment. Any  
kind soul can tell me how to solve?

---

<div class="post-metadata">

**Author:** ![kenle545](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/278dde/32.png) [@kenle545](https://www.ruby-forum.com/u/kenle545)\
**Post date:** [February 22, 2009, 12:42am UTC](https://www.ruby-forum.com/t/undefined-method-user-id/160979/2 "2009-02-22T00:42:34Z")

</div>

On Feb 21, 5:46 pm, Ken L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It said that the user\_id is undefined when creating a new comment. Any  
> kind soul can tell me how to solve?

Do your comments have a user\_id column ?

Fred
