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
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
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
(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)
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>
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.
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.
(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.