You have a nil object

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

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

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

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

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

@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: </tr> 9: 10: <% for job in @jobs %> 11: <tr> 12: <% for column in Job.content_columns %> 13: <td><%=h job.send(column.name) %></td>

hope this could be any help

Simon.

@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

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

Fred

7: <% end %> 8: </tr> 9: 10: <% for job in @jobs %> 11: <tr> 12: <% for column in Job.content_columns %> 13: <td><%=h job.send(column.name) %></td>

sorry that was the error for when it was @job, this is the one for @jobs

19: <% end %> 20: </table> 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: <br />

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

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