ruby-on-railsruby

Ruby on rails no method error controller home


NoMethodError in MainController#home undefined method `id' for nil:NilClass Extracted source (around line #62)

This is the snippet where the error seem to be originated

by_s = WorkflowStateTransition.select(:workflow_state_id).where(by_supervisor: true).collect{|x| x.workflow_state_id }
wp = WorkflowProcess.find_by(workflow: 'Timesheet')
wpr= WorkflowProcess.find_by(workflow: 'Petty Cash Request')
    
by_s = WorkflowStateTransition.joins("INNER JOIN workflow_states ON workflow_states.workflow_state_id = workflow_state_transitions.workflow_state_id")
                                    .where("workflow_states.workflow_process_id = ? AND workflow_state_transitions.by_supervisor = ?", wp.id, true)
                                    .collect{|x| x.workflow_state_id }
by_s_r = WorkflowStateTransition.joins("INNER JOIN workflow_states ON workflow_states.workflow_state_id = workflow_state_transitions.workflow_state_id")
                                      .where("workflow_states.workflow_process_id = ? AND workflow_state_transitions.by_supervisor = ?", wpr.id, true)
                                      .collect{|x| x.workflow_state_id }

Solution

  • You are calling wp.id and wpr.id in the below queries. Depending on in which line the exception is raised, wp or wpr is nil.

    Those variables can only be nil when one or both of these queries do not return any record:

    wp  = WorkflowProcess.find_by(workflow: 'Timesheet')
    wpr = WorkflowProcess.find_by(workflow: 'Petty Cash Request')
    

    I suggest checking in your DB why those records do not exist.