Hello. We are currently setting up the association.
We are building a 1: many relationship with worker: lists and would like to display it if there is worker.user_name in lists/show.
But if workers do not exist
undefined method ʻuser_name'for nil: NilClass
Error has occurred and cannot be resolved.
References: worker is specified, and the following settings are also made in model.
def show @list = List.find (params [: id]) @q = List.ransack (params [: q]) @lists = @ q.result @prev_list = @ lists.where ("lists.id<?", @ List.id) .last @next_list = @ lists.where ("lists.id>?", @ List.id) .first end private def list_params params.require (: list) .permit ( : company, # company name )&.merge (worker: current_worker) end
= @ list.worker.user_name
Is there a mistake in the above program?
I look forward to working with you.
Answer # 1
Because it's Rails 5
belongs_to: workerThen automatically
existA validation of the worker_id setting for the worker is created.
Despite this, if that happens, the possibility is
List created before setting belogns_to
It is a List that has no foreign_key setting in the DB and is inserted without passing through Rails (maybe it is defined because it is references: worker).
So, delete those data as they will no longer occur (because it will be validated).
For symptomatic treatment
worker&.user_nameThere is also a hand.
- ruby on rails 6 - about rails error "wrong number of arguments (given 1, expected 0)"
- ruby - about error of update action when nesting
- ruby on rails - about the error when trying to implement jquery with rails ($is not defined;please fix or add/* global * /)
- ruby - about the error content displayed in "rails" rspec
- ruby on rails 5 - about lint/syntax error of rubocop
- about ruby error statement
- ruby on rails 5 - error about rails where method
- ruby - [rails] about the error that occurs when deleting likes (routing error)
- ruby - about syntax error when running the calendar program
- about java500 error
- ruby on rails - error when vagrant up
- ruby on rails - uncaught reference error: $is not defined cannot be resolved
- ruby on rails - about request spec of rspec about test code at login
- about redis timeout error on heroku
- ruby - about jquery cookies
- ruby - i get an error when testing rspec instance variables
- about setting error to run circleci rspec
- ruby - i want to cache the association for user and make it a thin controller
- ruby - about rails controller new action
- about ruby exception method stackrace
- html - i want to allow users to use points
- ruby - how to create a seed file associated with user
- ruby - about implementation of tag function in rails
- ruby - i want to display only the image of the logged-in user when executing the search function
- ruby - [rails] i can't install the add favorite button
- how to unlock excel with password with ruby
- ruby - i want to display an error message when posting/editing fails by validation check
- ruby - [rails] when rails s is executed, an error "cannot load database configuration" appears and it is forcibly term