我在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'
activerecord (4.0.0) lib/active_record/relation/delegation.rb:68:in `method_missing'
app/controllers/api/v1/superusers.rb:17:in `block (2 levels) in <class:Users>'
grape (0.7.0) lib/grape/endpoint.rb:31:in `call'
grape (0.7.0) lib/grape/endpoint.rb:31:in `block in generate_api_method'
grape (0.7.0) lib/grape/endpoint.rb:407:in `call'
grape (0.7.0) lib/grape/endpoint.rb:407:in `run'
grape (0.7.0) lib/grape/endpoint.rb:156:in `block in call!'
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call'
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call!'
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call'
grape (0.7.0) lib/grape/middleware/base.rb:24:in `call!'
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call'
grape (0.7.0) lib/grape/middleware/error.rb:26:in `block in call!'
grape (0.7.0) lib/grape/middleware/error.rb:25:in `catch'
grape (0.7.0) lib/grape/middleware/error.rb:25:in `call!'
grape (0.7.0) lib/grape/middleware/base.rb:18:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
rack (1.5.2) lib/rack/builder.rb:138:in `call'
grape (0.7.0) lib/grape/endpoint.rb:157:in `call!'
grape (0.7.0) lib/grape/endpoint.rb:145:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
grape (0.7.0) lib/grape/api.rb:537:in `call'
grape (0.7.0) lib/grape/api.rb:44:in `call!'
grape (0.7.0) lib/grape/api.rb:40:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__3113182128831505771__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/ruben/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
错误发生是因为您试图在ActiveRecord::Relation元素上调用:id方法。
尝试:
@your_element.first.id