# How to call handle multiple models in one model in rails

**URL:** <https://www.ruby-forum.com/t/how-to-call-handle-multiple-models-in-one-model-in-rails/259934>\
**Category:** Uncategorized\
**Created:** [July 3, 2020, 4:17am UTC](https://www.ruby-forum.com/t/how-to-call-handle-multiple-models-in-one-model-in-rails/259934 "2020-07-03T04:17:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@shang](https://www.ruby-forum.com/u/shang)\
**Post date:** [July 3, 2020, 4:17am UTC](https://www.ruby-forum.com/t/how-to-call-handle-multiple-models-in-one-model-in-rails/259934/1 "2020-07-03T04:17:15Z")

</div>

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](http://prntscr.com/tau6t6)  
I’m not sure what wrong is… Please let me know if you know this problem. Many thanks.

---

<div class="post-metadata">

**Author:** ![Paulo\_Felipe](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/paulo_felipe/32/2428_2.png) [@Paulo\_Felipe](https://www.ruby-forum.com/u/Paulo_Felipe)\
**Post date:** [July 7, 2020, 1:19pm UTC](https://www.ruby-forum.com/t/how-to-call-handle-multiple-models-in-one-model-in-rails/259934/2 "2020-07-07T13:19:36Z")

</div>

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.
