# Newbie problem associating user with created objects

**URL:** <https://www.ruby-forum.com/t/newbie-problem-associating-user-with-created-objects/145769>\
**Category:** Rails\
**Created:** [August 13, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/newbie-problem-associating-user-with-created-objects/145769 "2008-08-13T20:24:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@Smerd](https://www.ruby-forum.com/u/Smerd)\
**Post date:** [August 13, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/newbie-problem-associating-user-with-created-objects/145769/1 "2008-08-13T20:24:33Z")

</div>

I’m embarrassed that I can’t figure this out on my own. My only excuse  
is that I’m just a few weeks into rails…

So, I’ve got restful\_authentication all up a running fine. I’ve got an  
“Idea” model, and all I want is for users to be associated with the  
“ideas” they create. There’s no problem creating new ideas, but for  
some reason I can’t get them associated with the user! When I try to  
access @idea.user.login through the view, I get the error, “You have a  
nil object when you didn’t expect it! The error occurred while  
evaluating nil.login”

The relevant Idea Controller part:

1. def create
2. 

```
@idea = @current_user.ideas.build params[:idea]

```

3. 

```
@idea.create_rating ## I have a simple rating system

```

too…without this in there, the view freaks out if it tries display  
the rating of an idea that has not yet been rated–I have vague sense  
that this is what’s causing my current problem…  
4.  
5. respond\_to do |format|  
6. if @idea.save  
7. flash[:notice] = ‘Idea was successfully created.’  
8. format.html { redirect\_to(@idea) }  
9. format.xml { render :xml =\> @idea, :status  
=\> :created, :location =\> @idea }  
10. else  
11. format.html { render :action =\> “new” }  
12. format.xml { render :xml =\> @idea.errors, :status  
=\> :unprocessable\_entity }  
13. end  
14. end  
15. end

The relevant view code is simple:

1. \<%= @idea.user.login %\>

Some associations:  
User:  
has\_many :ideas

Idea:  
belongs\_to :user  
has\_one :rating

Rating:  
belongs\_to :idea

A couple extra pieces of information: if this part of the view code is  
removed, the view displays all other attributes of model just fine.  
And here’s the kicker: if I go in rails console and create a new User,  
‘u’, and then type in " i = u.ideas.build ‘, the console will spit out  
the appropriate information when I type ’ i.user.login.’ Bah–why in  
the console but not in the browser?! Can some more educated rails  
developer help me out? Thanks!
