1

I'm trying to get an active record association working for two tables that do not have a plural form but still can imply multiple ( Equipment and JobEquipment)

I keep getting the following error when I try and get the Equipment from a the JobEquipment object. The relationship is 1 to Many ( 1 piece of equipment can have Many Job Equipment instances)

job_equipment.equipment

NoMethodError: undefined method `fetch_value' for nil:NilClass

Currently my models look like this

class JobEquipment < ApplicationRecord
    self.table_name = "job_equipment"
    belongs_to :equipment, :class_name=>"Equipment"
    // I tried adding the :class_name=>model to indicate to active record that this would be the plural name as well but no luck.

end

class Equipment < ApplicationRecord
    self.table_name = 'equipment'
    has_many :job_equipment, :class_name => "JobEquipment" // I don't believe this has_many association is required but I was running out of ideas so I've added it just incase.
end

The JobEquipment table has the equipment_id.

What am I missing here? All my other associations work but the tables all have plural names. I've tried a few things to get this to work. I even tried to use Equipments and JobEquipments but Rails/Active record still threw an error.

I feel like there must be a way to do this as I think a table named equipment would be very common.

Any help would be much appreciated, strangely I couldn't find any resources indicating others having issues with associations on non-plural form tables

SegFaultDev
  • 455
  • 1
  • 7
  • 24

0 Answers0