# Beginner problem with foreign keys

**URL:** <https://www.ruby-forum.com/t/beginner-problem-with-foreign-keys/58172>\
**Category:** Rails\
**Created:** [April 19, 2006, 8:40am UTC](https://www.ruby-forum.com/t/beginner-problem-with-foreign-keys/58172 "2006-04-19T08:40:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![printmkr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9f8e36/32.png) [@printmkr](https://www.ruby-forum.com/u/printmkr)\
**Post date:** [April 19, 2006, 8:40am UTC](https://www.ruby-forum.com/t/beginner-problem-with-foreign-keys/58172/1 "2006-04-19T08:40:07Z")

</div>

Hi, I’ve been trying to get this to work all night. I’ve made some  
progress by reading other posts, but I’m stuck now and can’t figure out  
what I am doing wrong.

I have two tables: application\_users and user\_types. In my  
application\_users table I have a field named “user\_type\_id”. At first,  
I thought if I set up the foreign key with the correct names, it would  
automatically appear in my “new” form, but I got past that issue.

My application\_user model looks like this:

class ApplicationUser \< ActiveRecord::Base  
belongs\_to :user\_type  
validates\_presence\_of :first\_name, :last\_name, :user\_type\_id,  
:login\_name, :password  
end

My user\_type model looks like this:

class UserType \< ActiveRecord::Base  
has\_many :application\_users  
end

In my controller, my new method looks like this:

def new  
@application\_user = ApplicationUser.new  
@user\_types = UserType.find\_all  
end

And I modified the new.rhtml view to contain this select list:

User type  
 \<% @user\_types.each do |user\_type| %\> \<%= user\_type.user\_type %\> \<% end %\>

The select list fills with the data from my user\_types table, but when I  
submit the form I get the famous “nil object” error under “NoMethodError  
in Admin#create”. The extracted source shows my code for the select  
list in new.rhtml, specifically the “\<% @user\_types.each do |user\_type|  
%\>” line.

My create method in my controller looks correct to me:

def create  
@application\_user = ApplicationUser.new(params[:application\_user])  
if @application\_user.save  
flash[:notice] = ‘User was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

I figured that there was some problem with my validation and it was  
dying when it got sent back to the new form. I commented out the  
validation and the form worked correctly. But if I put back the  
validation, even just testing the presence of one field, and I don’t  
fill out that field, I get the same error.

It’s obvious to me that the problem occurs when the application\_user  
object can’t be saved, and it gets sent back to the form where the  
user\_types object is now nil. But I don’t understand why. It seems  
that when “application\_user.save” fails, it goes to “render :action =\>  
‘new’” where @user\_types should be initialized. The new method works  
the first time I hit the form, why does it not work when it is called  
from the create method?

Thanks for any help you can give me - this is driving me crazy.

---

<div class="post-metadata">

**Author:** ![printmkr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9f8e36/32.png) [@printmkr](https://www.ruby-forum.com/u/printmkr)\
**Post date:** [April 19, 2006, 9:20am UTC](https://www.ruby-forum.com/t/beginner-problem-with-foreign-keys/58172/2 "2006-04-19T09:20:29Z")

</div>

Hi !

2006/4/19, Bill C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> It’s obvious to me that the problem occurs when the application\_user  
> object can’t be saved, and it gets sent back to the form where the  
> user\_types object is now nil. But I don’t understand why. It seems  
> that when “application\_user.save” fails, it goes to “render :action =\>  
> ‘new’” where @user\_types should be initialized. The new method works  
> the first time I hit the form, why does it not work when it is called  
> from the create method?

When you call render :action =\> :new, this only renders. It doesn’t  
execute the logic in the #new method.

To resolve that problem, you have a few solutions:

1. Add a before\_filter that will load @user\_types when necessary
2. Copy/paste the line (big frown from the community)
3. Make the view find the user types itself (lesser frown)

Hope that helps !

---

<div class="post-metadata">

**Author:** ![printmkr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9f8e36/32.png) [@printmkr](https://www.ruby-forum.com/u/printmkr)\
**Post date:** [April 20, 2006, 12:28am UTC](https://www.ruby-forum.com/t/beginner-problem-with-foreign-keys/58172/3 "2006-04-20T00:28:14Z")

</div>

FranÃ§ois Beausoleil wrote:

> Hi !
> 
> When you call render :action =\> :new, this only renders. It doesn’t  
> execute the logic in the #new method.
> 
> To resolve that problem, you have a few solutions:
> 
> 1. Add a before\_filter that will load @user\_types when necessary
> 2. Copy/paste the line (big frown from the community)
> 3. Make the view find the user types itself (lesser frown)
> 
> Hope that helps !

Thanks a lot. The before\_filter is exactly what I was looking for. I  
hadn’t gotten that far in the book yet. 😉
