views:

591

answers:

2

I'm at a loss here. When running in Dev mode (script/server) I the error "undefined local variable or method 'id' for #<InsertModelNameHere:0x7f19bdb87dc8>" at random times when I try to list entries for a number of my models. If I refresh the page it works fine and I'm assuming that I only see it in dev because everything gets reloaded for each request. Maybe a race condition or something going on? I'm not even referencing the "id" attribute (legacy table that uses a different key naming convention) in the views where this is an issue, One of my co-workers informed me that rails will create an "id" attribute based on the primary key defined in the model though.

Below is the code for the view, the error message,a and the stack trace. I'm running rails 2.3.3.

View:

<h2 class="textCenter">To Do List</h2>
<% form_tag({:action => "list"}, :method => "get") do %>
 <div class="full_width textCenter">
  <%= text_field_tag "q", params[:q] %>
  <input type="submit" value="Search" />
 </div>
<% end %>
<table class="full_width list">
 <thead >
  <tr >
   <th >ID</th>
   <th >Name</th>
   <th >Resource ID</th>
   <th >Company</th>
   <th >&nbsp;</th>
   <th >&nbsp;</th>
  </tr>
 </thead>
 <tbody >
  <% begin %>
   <% @todos.each do |todo| %> 
    <tr class="<%= cycle "striped", "" %>" >
     <td ><%= todo.pk_todo_id %></td>
     <td ><%= todo.name %></td>
     <td ><%= todo.fk_resource_id %></td>
     <td ><%= todo.fk_company_id %></td>
     <td ><%= link_to "STA", {:controller => "stas", :action => "list", :id => todo.fk_resource_id} %></td>
     <td ><%= link_to "Edit", {:controller => "todos", :action => "edit", :id => todo.pk_todo_id} %></td>
    </tr>
   <% end %>
  <% rescue => e; puts e.message; puts e.backtrace %>
  <% end %>
 </tbody>
</table>

Error: undefined local variable or method 'id' for #<Todo:0x7f19bdb87dc8>

/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:260:in 'method_missing'
/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/attribute_methods.rb:249:in 'method_missing'
/home/scott/Projects/cllsuite/trunk/app/views/todos/list.rhtml:25:in '_run_rhtml_app47views47todos47list46rhtml'
/home/scott/Projects/cllsuite/trunk/app/views/todos/list.rhtml:23:in 'each'
/home/scott/Projects/cllsuite/trunk/app/views/todos/list.rhtml:23:in '_run_rhtml_app47views47todos47list46rhtml'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/renderable.rb:34:in 'send'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/renderable.rb:34:in 'render'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:301:in 'with_template'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/renderable.rb:30:in 'render'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/template.rb:199:in 'render_template'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:260:in 'render'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:343:in '_render_with_layout'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_view/base.rb:257:in 'render'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1246:in 'render_for_file'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:941:in 'render_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in 'render'
/var/lib/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in 'ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in 'realtime'
/var/lib/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in 'ms'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in 'render'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1322:in 'default_render'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1328:in 'perform_action_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:617:in 'call_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:610:in 'perform_action_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in 'perform_action_without_rescue'
/var/lib/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in 'ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in 'realtime'
/var/lib/gems/1.8/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in 'ms'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in 'perform_action_without_rescue'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/rescue.rb:160:in 'perform_action_without_flash'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/flash.rb:146:in 'perform_action'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in 'send'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in 'process_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:606:in 'process'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:391:in 'process'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:386:in 'call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/routing/route_set.rb:434:in 'call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:88:in 'dispatch'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:111:in '_call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:82:in 'initialize'
/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:29:in 'call'
/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:29:in 'call'
/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in 'cache'
/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:9:in 'cache'
/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/query_cache.rb:28:in 'call'
/var/lib/gems/1.8/gems/activerecord-2.3.3/lib/active_record/connection_adapter /abstract/connection_pool.rb:361:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/head.rb:9:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/methodoverride.rb:24:in 'call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/params_parser.rb:15:in 'call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/session/cookie_store.rb:93:in 'call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/reloader.rb:29:in 'call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/failsafe.rb:26:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in 'synchronize'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/lock.rb:11:in 'call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/dispatcher.rb:106:in 'call'
/var/lib/gems/1.8/gems/rails-2.3.3/lib/rails/rack/static.rb:31:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:46:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in 'each'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/urlmap.rb:40:in 'call'
/var/lib/gems/1.8/gems/rails-2.3.3/lib/rails/rack/log_tailer.rb:17:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/content_length.rb:13:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/chunked.rb:15:in 'call'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:61:in 'process'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in 'process_client'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in 'each'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in 'process_client'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in 'run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in 'initialize'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in 'new'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in 'run'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in 'initialize'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in 'new'
/var/lib/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in 'run'
/var/lib/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:34:in 'run'
/var/lib/gems/1.8/gems/rails-2.3.3/lib/commands/server.rb:111
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in 'gem_original_require'
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in 'require'
script/server:3
+2  A: 

Hi Scott,

Have you set the primary_key on Todo?

class Todo
  set_primary_key 'pk_todo_id'

end

I think that may be your issue. Let me know what happens.

Good luck!

mixonic
Yeah, I've got that set already
This also doesn't happen every time I hit the veiw. It happens maybe half of the time.
Hey Scott, so maybe this is a 2.3.3 issue. See: https://rails.lighthouseapp.com/projects/8994/tickets/2948-exception-a-copy-of-actorscontroller-has-been-removed-from-the-module-tree-but-is-still-active and a possible fix in http://github.com/rails/rails/commit/d37ac7958fc88fdbf37a8948102f6b4e45c530b3. We had this problem, apparently it doesn't happen in production mode or under the thin server. Try using thin, or try to recreate it in production mode. The behavior is that classes are reloaded incorrectly, so a stub of Todo is loaded instead of the real class (so your primary key is skipped).
mixonic
Having trouble applying the patch, will see if it fixed it when I finally get it to work
A: 

I'm going to revert back to 2.3.2 since I can't get that patch to apply correctly. I'm going to assume that's the issue, especially since it's specific to 2.3.3. Thanks for the help everyone.

Yeah we've just been working around it by using thin to start our dev servers directly. 2.3.3 was a messy release.
mixonic