# Undefined method

**URL:** <https://www.ruby-forum.com/t/undefined-method/94175>\
**Category:** Rails\
**Created:** [April 24, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/undefined-method/94175 "2006-04-24T13:26:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 24, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/undefined-method/94175/1 "2006-04-24T13:26:51Z")

</div>

Hi,

I’m new to rails and have stumbled across this error. I’m not sure where  
I am supposed to define the method user\_id . I have included the  
offending code and my controller. Can anyone help?!

NoMethodError in Registeredphone#new

Showing app/views/registeredphone/new.rhtml where line #15 raised:

undefined method `user\_id’ for #Registeredphone:0x3874030

Extracted source (around line #15):

12:   
13: \<% @user.each do |user| %\>  
14: \<option value="\<%= user.id %\>"  
15: \<%= ‘selected’ if user.id == @registeredphone.user\_id %\>\>  
16: \<%= user.txtForename %\>  
17:   
18: \<% end %\>

My controller is as follows:

class RegisteredphoneController \< ApplicationController  
layout “standard-layout”  
scaffold :registeredphone

```
def delete
	Registeredphone.find(@params['id']).destroy
	redirect_to :action => 'list'
end

def create
	@registeredphone = Registeredphone.new(@params['registeredphone'])
	@registeredphone.txtregisterdatetime = Time.now
	if @registeredphone.save
		redirect_to :action => 'list'
	else
		render_action 'new'
	end
end

def new
	@registeredphone = Registeredphone.new
	@user = User.find_all
end

def list
	@user = @params['user']
	@registeredphones = Registeredphone.find_all
end

def edit
	@registeredphone = Registeredphone.find(@params["id"])
	@user = User.find_all
end

```

end

Any help would be appreciated!!  
Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 24, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/undefined-method/94175/2 "2006-04-24T13:35:04Z")

</div>

On Apr 24, 2006, at 13:26, Alana Murphy wrote:

> undefined method `user\_id’ for #Registeredphone:0x3874030  
> This error message means that a method “user\_id” is being called on  
> an instance of the class Registeredphone, and such a method is  
> unknown by instances of said class. The trace suggests the offending  
> code is around line 15 of “app/views/registeredphone/new.rhtml”.

> 15: \<%= ‘selected’ if user.id == @registeredphone.user\_id %\>\>

Yeah, right there. You need to decide whether that should be a valid  
call, and if it should why the method is actually not defined.

– fxn

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 24, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/undefined-method/94175/3 "2006-04-24T14:22:31Z")

</div>

As I’m new to rails and still learning my way, I’m not sure why the  
method is not defined or where it should be defined. My model is as  
follows:

class Registeredphone \< ActiveRecord::Base  
set\_table\_name “tblregisteredphones”  
set\_primary\_key “TblRegisteredPhonesID”  
belongs\_to :user, :foreign\_key =\> “intUserID”  
end

Is this where I should be defining ‘user\_id’? The foreign key in this  
table (intUserID)is linked to the id in the user table (TblUserID) and  
has been aliased due to difficult naming conventions used in this legacy  
schema. Do I need to do more, or is this alias method correct?

Thank you,

Alana

Xavier N. wrote:

> On Apr 24, 2006, at 13:26, Alana Murphy wrote:
> 
> > undefined method `user\_id’ for #Registeredphone:0x3874030  
> > This error message means that a method “user\_id” is being called on  
> > an instance of the class Registeredphone, and such a method is  
> > unknown by instances of said class. The trace suggests the offending  
> > code is around line 15 of “app/views/registeredphone/new.rhtml”.
> 
> > 15: \<%= ‘selected’ if user.id == @registeredphone.user\_id %\>\>
> 
> Yeah, right there. You need to decide whether that should be a valid  
> call, and if it should why the method is actually not defined.
> 
> – fxn

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 24, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/undefined-method/94175/4 "2006-04-24T17:48:06Z")

</div>

Thanks for your help, its all up and running now!

Alana

---

<div class="post-metadata">

**Author:** ![alana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@alana](https://www.ruby-forum.com/u/alana)\
**Post date:** [April 24, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/undefined-method/94175/5 "2006-04-24T14:46:46Z")

</div>

ActiveRecord is expectinb your registered\_phone table to contain a  
column named ‘user\_id’. It seems you are using ‘intUserID’ to keep  
track of the relationship, so if you change ‘user\_id’ in your view to  
‘intUserID’, it should work.

On Monday, April 24, 2006, at 2:22 PM, Alana Murphy wrote:

> > On Apr 24, 2006, at 13:26, Alana Murphy wrote:  
> > call, and if it should why the method is actually not defined.
> > 
> > – fxn
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin
