i have this in my template
<% @clients.each do |client| %>
<li><%= link_to client.name, :controller => "client", :action => "show", :id => client.id %></li>
<%=YAML::dump(client.lastfull)%>
<% end %>
where client looks like this:
class Client < ActiveRecord::Base
set_table_name 'client'
alias_attribute :id, :clientid
set_primary_key :clientid
has_many :jobs, :foreign_key => 'clientid', :order => 'starttime DESC'
def lastfull
jobs.first
end
end
this works and this is output :
--- !ruby/object:Job attributes: jobid: "81" name: dobrak comment: "" endtime: 2012-06-20 10:15:04
But when i try to read any of the attributes i get error:
undefined method `jobid' for nil:NilClass
Job class:
class Job < ActiveRecord::Base
attr_accessor :jobid
set_table_name 'job'
belongs_to :client
has_one :status, :primary_key => 'jobstatus', :foreign_key => 'jobstatus'
end
i tried to add method jobid which return attribute and adding attr_accessor, but nothing worked for me. Any suggestions? Thank you.
Rails 2.3.5
It looks like there are clients without any jobs. What happens if you change the line to something like
<%= client.lastfull.present? ? YAML::dump(client.lastfull.jobid) : 'client has no jobs' %>? You can also take a look atObject#try