Rails NoMethodError (未定义的方法 'id'...) 同时使用 'protected attributes' gem



我在Rails中使用'protected_attributes' gem,它将显式的attr_accessible带回Rails 4(我需要与另一个gem兼容,长话短说)。

在编写代码的过程中,我试图访问一个未命名为可访问的属性。我收到以下错误:

NoMethodError (undefined method `id' for #<ActiveRecord::Relation::ActiveRecord_Relation_User:0x00000104c51c00>):

意识到我的错误,我添加了:id与我的attr_accessible列表的其余部分。但是,即使在重新启动rails服务器之后,每次运行代码时仍然会得到完全相同的错误。这发生在我之前,我所做的是一个rake db:reset,不管出于什么原因,解决了我的问题(但在这个过程中核弹我的db)。我可不想再清空我的数据库了……有更简单的方法来解决这个问题吗?是我没清理缓存还是怎么的?谢谢。

编辑:对于感兴趣的人,这是完整的跟踪:

activerecord (4.0.0) lib/active_record/relation/delegation.rb:121:in `method_missing&#39;
activerecord (4.0.0) lib/active_record/relation/delegation.rb:68:in `method_missing&#39;
app/controllers/api/v1/superusers.rb:17:in `block (2 levels) in &lt;class:Users&gt;&#39;
grape (0.7.0) lib/grape/endpoint.rb:31:in `call&#39;
grape (0.7.0) lib/grape/endpoint.rb:31:in `block in generate_api_method&#39;
grape (0.7.0) lib/grape/endpoint.rb:407:in `call&#39;
grape (0.7.0) lib/grape/endpoint.rb:407:in `run&#39;
grape (0.7.0) lib/grape/endpoint.rb:156:in `block in call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call&#39;
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call&#39;
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call&#39;
grape (0.7.0) lib/grape/middleware/error.rb:26:in `block in call!&#39;
grape (0.7.0) lib/grape/middleware/error.rb:25:in `catch&#39;
grape (0.7.0) lib/grape/middleware/error.rb:25:in `call!&#39;
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call&#39;
rack (1.5.2) lib/rack/head.rb:11:in `call&#39;
rack (1.5.2) lib/rack/builder.rb:138:in `call&#39;
grape (0.7.0) lib/grape/endpoint.rb:157:in `call!&#39;
grape (0.7.0) lib/grape/endpoint.rb:145:in `call&#39;
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call&#39;
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize&#39;
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in `optimized_each&#39;
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize&#39;
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call&#39;
grape (0.7.0) lib/grape/api.rb:537:in `call&#39;
grape (0.7.0) lib/grape/api.rb:44:in `call!&#39;
grape (0.7.0) lib/grape/api.rb:40:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call&#39;
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each&#39;
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call&#39;
rack (1.5.2) lib/rack/etag.rb:23:in `call&#39;
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call&#39;
rack (1.5.2) lib/rack/head.rb:11:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context&#39;
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call&#39;
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call&#39;
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call&#39;
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call&#39;
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__3113182128831505771__call__callbacks&#39;
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call&#39;
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app&#39;
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call&#39;
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged&#39;
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged&#39;
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged&#39;
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call&#39;
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call&#39;
rack (1.5.2) lib/rack/runtime.rb:17:in `call&#39;
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call&#39;
railties (4.0.0) lib/rails/engine.rb:511:in `call&#39;
railties (4.0.0) lib/rails/application.rb:97:in `call&#39;
rack (1.5.2) lib/rack/lock.rb:17:in `call&#39;
rack (1.5.2) lib/rack/content_length.rb:14:in `call&#39;
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service&#39;
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service&#39;
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run&#39;
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread&#39;

错误发生是因为您试图在ActiveRecord::Relation元素上调用:id方法。

尝试:

@your_element.first.id

相关内容

  • 没有找到相关文章

最新更新