Rails "неопределенная локальная переменная или метод" для attr, который даже не связан - PullRequest
2 голосов
/ 05 августа 2009

Я в растерянности здесь. При работе в режиме Dev (скрипт / сервер) я выдает ошибку «undefined local variable or method 'id' for #<InsertModelNameHere:0x7f19bdb87dc8>» в случайное время, когда пытаюсь составить список записей для ряда моих моделей. Если я обновляю страницу, она работает нормально, и я предполагаю, что вижу ее только в dev, потому что все перезагружается для каждого запроса. Может быть, состояние гонки или что-то происходит? Я даже не ссылаюсь на атрибут «id» (устаревшая таблица, которая использует другое соглашение об именах ключей) в представлениях, где это является проблемой. Один из моих коллег сообщил мне, что rails создаст атрибут «id» на основе хотя первичный ключ определен в модели.

Ниже приведен код для представления, сообщения об ошибке и трассировки стека. Я бегу по рельсам 2.3.3.

Вид:

<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>

Ошибка: 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 ]

2 голосов
/ 05 августа 2009

Вы установили primary_key на Todo?

class Todo
  set_primary_key 'pk_todo_id'

end

Я думаю, что это может быть вашей проблемой. Дайте мне знать, что происходит.

Удачи!

0 голосов
/ 05 августа 2009

Я собираюсь вернуться к 2.3.2, так как не могу правильно применить этот патч. Я собираюсь предположить, что это проблема, тем более что она специфична для 2.3.3. Спасибо всем за помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...