我正在尝试在rails中创建一个似乎导致一些问题的关系。我还将添加此问题似乎只出现在测试环境中,但问题是我们无法推送新代码失败。
我们有以下型号:
class User < ActiveRecord::Base
has_many :enrolled_minors
end
次要
class Minor < ActiveRecord::Base
has_many :enrolled_minors
end
注册未成年人
class EnrolledMinor < ActiveRecord::Base
belongs_to :user
belongs_to :minor
end
因此,用户有未成年人(未成年人),并且该关系通过已登记未成年人建模(也省略了更多信息)。以下是我创建注册小调的方法
@enrolled_minor = user.enrolled_minors.new(enrolled_minor_params)
def enrolled_minor_params
params.require(:enrolled_minor).permit(:user_id, :minor_id)
end
索引视图的一部分
- if @user.enrolled_minors.count > 0 || current_user.is_admin
- @user.enrolled_minors.each do |em|
= puts "user info: #{@user.inspect}"
= puts "em: #{em.inspect}"
= puts "manual-minor: #{Minor.find(em.minor_id).inspect}"
= puts "em-minor: #{em.minor.inspect}"
.minor-container data-enrolled-minor-id=em.id
.minor-overview
.container
= render partial: 'dashboard/overview', locals: {em: em}
部分显示故障(在h2线上失败)
- if em
h2 #{em.minor.name}
来自put的输出
user info: #<User id: 2, first_name: "user3", last_name: "last_name3", created_at: "2017-01-10 02:35:36", updated_at: "2017-01-10 02:35:36">
em: #<EnrolledMinor id: 1, minor_accepted: nil, minor_completed: nil, user_id: 2, created_at: "2017-01-10 02:35:37", updated_at: "2017-01-10 02:35:37", minor_id: 2>
manual-minor: #<Minor id: 2, name: "minor4", nickname: "m4">
em-minor: nil
堆栈跟踪
undefined method `name' for nil:NilClass (ActionView::Template::Error)
/Users/ajk292/Documents/code/rails/dyson-minors/app/views/dashboard/_overview.html.slim:2:in `_app_views_dashboard__overview_html_slim__2246120880992646294_70207059823680'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/template.rb:145:in `block in render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:166:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/template.rb:333:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/template.rb:143:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `block in instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/partial_renderer.rb:309:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/renderer.rb:47:in `render_partial'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/renderer.rb:21:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/helpers/rendering_helper.rb:32:in `render'
/Users/ajk292/Documents/code/rails/dyson-minors/app/views/dashboard/index.html.slim:10:in `block in _app_views_dashboard_index_html_slim__906501886411520268_70207062528140'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/delegation.rb:46:in `each'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/delegation.rb:46:in `each'
/Users/ajk292/Documents/code/rails/dyson-minors/app/views/dashboard/index.html.slim:2:in `_app_views_dashboard_index_html_slim__906501886411520268_70207062528140'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/template.rb:145:in `block in render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:166:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/template.rb:333:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/template.rb:143:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `block in instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/template_renderer.rb:52:in `render_template'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/template_renderer.rb:14:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/renderer.rb:42:in `render_template'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/renderer/renderer.rb:23:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/rendering.rb:100:in `_render_template'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/streaming.rb:217:in `_render_template'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/rendering.rb:83:in `render_to_body'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/rendering.rb:32:in `render_to_body'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/renderers.rb:37:in `render_to_body'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/abstract_controller/rendering.rb:25:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/rendering.rb:16:in `render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/ajk292/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/core_ext/benchmark.rb:12:in `ms'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:43:in `render'
/Users/ajk292/Documents/code/rails/dyson-minors/app/controllers/admin/users_controller.rb:13:in `show'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/abstract_controller/base.rb:198:in `process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/rendering.rb:10:in `process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:117:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:117:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:505:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:505:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/abstract_controller/callbacks.rb:19:in `process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/rescue.rb:29:in `process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `block in instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `instrument'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/abstract_controller/base.rb:137:in `process'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionview-4.2.4/lib/action_view/rendering.rb:30:in `process'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal.rb:196:in `dispatch'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_controller/metal.rb:237:in `block in action'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:76:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:45:in `serve'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/journey/router.rb:43:in `block in serve'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/journey/router.rb:30:in `each'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/journey/router.rb:30:in `serve'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:821:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/etag.rb:24:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/conditionalget.rb:25:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/flash.rb:260:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225:in `context'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/cookies.rb:560:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/query_cache.rb:36:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/cucumber-rails-1.4.2/lib/cucumber/rails/action_controller.rb:10:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/rack/logger.rb:38:in `call_app'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `block in call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:68:in `block in tagged'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:26:in `tagged'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/tagged_logging.rb:68:in `tagged'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/rack/logger.rb:20:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/request_store-1.3.0/lib/request_store/middleware.rb:9:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/request_id.rb:21:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/static.rb:116:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/engine.rb:518:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/application.rb:165:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/capybara-2.5.0/lib/capybara/server.rb:19:in `call'
/Users/ajk292/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
/Users/ajk292/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/ajk292/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/ajk292/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
所以我所做的就是我认为这个问题在某种程度上是未成年人与未成年人之间的关系。有一个参考,我可以&#34;手动&#34;通过使用find函数和存储的id找到它。另外正如我所说,这在运行测试时发生但在开发时没有。如果我在从Web开发服务器注册一个新的辅助页面后转到该页面,那么它就会成功。
有什么想法吗?
答案 0 :(得分:0)
您希望在关联上指定:inverse_of选项,以确保不会将同一对象的不同版本加载到内存中。
class User < ActiveRecord::Base
has_many :enrolled_minors, inverse_of: :user
end
class Minor < ActiveRecord::Base
has_many :enrolled_minors, inverse_of: :minor
end
class EnrolledMinor < ActiveRecord::Base
belongs_to :user, inverse_of: :enrolled_minors
belongs_to :minor, inverse_of: :enrolled_minors
end
然后,当构建用户的enrolled_minor关联时,您可能不希望同时设置user_id参数,因为它会覆盖关联中的用户,所以
def user_built_enrolled_minor_params
params.require(:enrolled_minor).permit(:minor_id)
end
然后
#build linked EnrolledMinor but don't save
@enrolled_minor = user.enrolled_minors.build(user_built_enrolled_minor_params)
#build linked EnrolledMinor and save
@enrolled_minor = user.enrolled_minors.create(user_built_enrolled_minor_params)