# You have a nil object

**URL:** <https://www.ruby-forum.com/t/you-have-a-nil-object/135723>\
**Category:** Rails\
**Created:** [April 24, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723 "2008-04-24T13:57:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/1 "2008-04-24T13:57:28Z")

</div>

Hi People  
I have just started using rails and am running into a few problems,

the basic setup I have is two tables, one called weburls and the other  
called jobs, and the relationship between them is one weburl can have  
many jobs, a job cant belong to multiple weburls

in the models I have (and im abit confused if they should be plural or  
not)

class Job \< ActiveRecord::Base  
belongs\_to:weburls  
end

and

class Weburl \< ActiveRecord::Base  
has\_many:Jobs  
end

I have a list of websites so that when you click on show it will take  
you to a page with a list of the jobs that are attached to it. the  
parameter is passed in the url like this

redirect\_to :controller =\>‘jobs’, :action =\> ‘list’, :weburls\_id =\>  
params[:id]

then in the def list in the jobs\_controller.rb I have

def list  
@job\_pages, @job = Job.find(:all, :conditions =\>[“weburls\_id = ?”,  
(params[:weburls\_id]])  
end

however the end result is You have a nil object when you didn’t expect  
it!  
You might have expected an instance of Array. The error occurred while  
evaluating nil.each

Any help would be appreciated.

Cheers

simon

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 2:05pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/2 "2008-04-24T14:05:12Z")

</div>

On 24 Apr 2008, at 12:57, Simon H. wrote:

> class Weburl \< ActiveRecord::Base  
> has\_many:Jobs  
> end

It’s a plural if the association can have many objects. So belongs\_to  
and has\_one are always singular, has\_many is always plural

> def list  
> @job\_pages, @job = Job.find(:all, :conditions =\>[“weburls\_id = ?”,  
> (params[:weburls\_id]])  
> end

That should be just

@jobs = Job.find(:all, :conditions =\>[“weburls\_id = ?”,  
params[:weburls\_id]])

(unless you wanted the paginate the results, in which case you’ll need  
to use the classic\_pagination or will\_paginate plugins. I’d get things  
working without first)

Fred

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/3 "2008-04-24T14:22:31Z")

</div>

> @jobs = Job.find(:all, :conditions =\>[“weburls\_id = ?”,  
> params[:weburls\_id]])

Hi Fred thanks for your help.

I have changed it to this can corrected the models, however im still  
getting the same error message. should the “@jobs” be just “@job” to  
match the others in the def list. I changed it quickly to see but with  
both variations it still comes up with the same error

the code snippet it gives is:

Extracted source (around line #10):

7: \<% end %\>  
8:   
9:  
10: \<% for job in @jobs %\>  
11:

  
12: \<% for column in Job.content\_columns %\>  
13: \<%=h job.send(column.name) %\>

hope this could be any help

Simon.

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/4 "2008-04-24T14:37:38Z")

</div>

> 7: \<% end %\>  
> 8:   
> 9:  
> 10: \<% for job in @jobs %\>  
> 11:
> 
>   
> 12: \<% for column in Job.content\_columns %\>  
> 13: \<%=h job.send(column.name) %\>
> 
> sorry that was the error for when it was @job, this is the one for @jobs

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/5 "2008-04-24T16:11:36Z")

</div>

On 24 Apr 2008, at 13:22, Simon H. wrote:

> match the others in the def list. I changed it quickly to see but with  
> both variations it still comes up with the same error

If you’re using @jobs in your view then you need to be setting @jobs  
somewhere in your controller.

Fred

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/6 "2008-04-24T14:37:57Z")

</div>

19: \<% end %\>  
20:   
21:  
22: \<%= link\_to ‘Previous page’, { :page =\> @job\_pages.current.previous  
} if @job\_pages.current.previous %\>  
23: \<%= link\_to ‘Next page’, { :page =\> @job\_pages.current.next } if  
@job\_pages.current.next %\>  
24:  
25:

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/7 "2008-04-24T16:13:30Z")

</div>

On 24 Apr 2008, at 13:37, Simon H. wrote:

> sorry that was the error for when it was @job, this is the one for  
> @jobs  
> I think it would help if you detailed the changes you’ve made, right  
> know we know you’ve change some things but we don’t actually know what  
> you’ve ended up with.

Fred

---

<div class="post-metadata">

**Author:** ![simonh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@simonh](https://www.ruby-forum.com/u/simonh)\
**Post date:** [April 24, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object/135723/8 "2008-04-24T16:30:39Z")

</div>

Simon,

One of the points of model associations is that you get association  
functionality.

Change your @jobs call to be:

@jobs = Weburl.find(params[:weburl\_id]).jobs

(or if the parameter is “weburls\_id” use that, although it should be  
singular if you are dealing with a single weburl)

The above line finds the weburl with the ID then uses the has\_many  
association to get the jobs for that weburl.

Otherwise, the view code looks fine. Looks like standard scaffold-  
generated code. If things are still being funky, I’d add a debugger  
statement before line 10 and then inspect the @jobs object. It should  
be an array of Job objects.

-Danimal

On Apr 24, 6:22 am, Simon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
