Issue to show data from relational database

I want to show data from two tables these are

applied_jobs table: it contains the below columns:

applied_jobs_id | jobseeker_id | expected_salary

jobseekers table: it contains the below columns:

jobseeker_id | year_of_experience | name

My view file codes:

 <tbody>
    <% @applicants_view.each do | applicants | %>
      <tr>
        <td><%=  applicants.jobseeker.name%></td>
        <td><%=

applicants.jobseeker.year_of_experience %>

<%= applicants.expected_salary %>
       </tr>
     <% end %>
  </tbody>

My controller codes:

def applicantsList

   @applicants_view = AppliedJob.paginate(page: params[:page],

:per_page => 4).order(‘applied_jobs_id DESC’)

end

My applied_job model codes:
class AppliedJob < ActiveRecord::Base

    belongs_to :jobseeker
end

My jobseeker model codes:

class Jobseeker < ActiveRecord::Base

    has_one :applied_job

end

When I try to show data it’s showing the below error

Showing

C:/wamp/www/hire_us/app/views/applicants_list/applicantsList.html.erb
where line #51 raised:

  undefined method `name' for nil:NilClass

 Rails.root: C:/wamp/www/hire_us
Application Trace | Framework Trace | Full Trace

app/views/applicants_list/applicantsList.html.erb:51:in block in _app_views_applicants_list_applicants_ist_html_erb___1439381291_141337560' app/views/applicants_list/applicantsList.html.erb:49:in_app_views_applicants_list_applicants_ist_html_erb___1439381291_141337560’

Someone can help me, please to identify where the bug?

One of your applicants has no jobseeker, it’s returning nil on
applicants.jobseeker

I have solved this issue by adding

<% @applicants_view.each do | applicants | %>
<% applicants.jobseeker.try(:name) %>

<% end %>

I think it will be good if share this answer in this forum so that if
anyone suffer this type of problem then it will be helpful.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs