How to call handle multiple models in one model in rails

Hi, I have one question. I’m developing an app using ruby on rails but I’m new on it. I have 3 models called unit.rb, prisoner.rb and volunteer.rb

  • unit.rb
    require ‘csv’

class Unit < ActiveRecord::Base
mount_uploader :scan, ScanUploader
mount_uploader :reader_scan, ReaderScanUploader
default_scope { order(mailing_date: :desc)}

Associations

belongs_to :course
belongs_to :prisoner
belongs_to :volunteer

Validations

validates :course, presence: true

validates :prisoner, presence: true

validates :mailing_date, presence: true

#validates :scan, presence: true

def self.to_csv
attributes = %w{course sub_course prisoner volunteer mailing_date return_date reader_mailing_date reader_return_date created_at}

CSV.generate(headers: true) do |csv|
  csv << attributes

  all.each do |unit|
    csv << attributes.map do |attr| 
      if attr == 'course'
        unit.course.try(:title)
      elsif attr == 'sub_course'
        unit.sub_course
      elsif attr == 'prisoner'
        unit.prisoner.name
      elsif attr == 'volunteer'
        unit.volunteer.name
      elsif attr == 'mailing_date'
        unit.mailing_date
      elsif attr == 'return_date'
        unit.return_date
      elsif attr == 'reader_mailing_date'
        unit.reader_mailing_date
      elsif attr == 'reader_return_date'
        unit.reader_return_date
      else
        unit.created_at
      end
    end
  end
end

end
end

  • prisoner.rb
    require ‘csv’

class Prisoner < ActiveRecord::Base
#Associations
has_many :units, inverse_of: :prisoner, dependent: :destroy
has_many :volunteers, through: :units
has_many :certificates, dependent: :destroy
has_and_belongs_to_many :courses

#validations
validates :first_name, presence: true
validates :last_name, presence: true
validates :prison_id, presence: true
validates :address_1, presence: true
validates :city, presence: true
validates :state, presence: true
validates :zip, presence: true

def name
	"#{first_name} #{last_name}"
end

end

  • volunteer.rb
    require ‘csv’

class Volunteer < ActiveRecord::Base
store_accessor :vacation

#Associations
has_many :units, inverse_of: :volunteer
has_many :prisoners, through: :units
has_and_belongs_to_many :courses

#Validations
validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :courses, presence: true

def name
“#{first_name} #{last_name}”
end
end

But when I click the download button, I’m facing 500 internal error in chrome console. And I’m facing this error via heroku logs command. Screenshot by Lightshot
I’m not sure what wrong is… Please let me know if you know this problem. Many thanks.

Hi, as your log file shows, there is a nil returned from unit controller, on name field.
Rails raise exception in case of any nil returned from any controller.
Before modify any controller, check exactly where the nil come from.
To do so, insert an if condition on your view above what you already have:
<% @units.each do %>
<% if @unit.name==nil%>
<%= here class unit has no name %>
Do it also where you think there may generate nil returns.
It will prevent rails to raise this exception.
Now, about your question, it seems you have already handled multiples models using one model.
To check that, open you console using ‘rails console’ command.
unit = Unit.all
prisioneer = Prisioner.all

Call ‘unit.first.prisioner’ to check if the database will return the relationship between them.