简体   繁体   中英

Rails NoMethodError (undefined method `id'…) while using 'protected attributes' gem

I'm using the 'protected_attributes' gem in Rails that brings back explicit attr_accessible to Rails 4 (which I needed for compatibility with another Gem, long story).

In the process of writing code, I tried to access an attribute that isn't named as accessible. I received the following error:

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

Realizing my mistake, I added :id with the rest of my attr_accessible list. However, even after restarting the rails server, I still get the same exact error every time I run the code. This had happened to me before and what I did was a rake db:reset which, for whatever reason, fixed my issues (but nuked my db in the process). I'd rather not wipe out my database again... Is there a simpler way to fix this? Some cache I'm not clearing or something? Thanks.

Edit: for those interested, this is the full trace:

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;

The error happens because you try to call :id method on ActiveRecord::Relation element.

Try to do:

@your_element.first.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM