0

I recently upgraded to rails 5.2 and now I am getting an error in one of my models because the belongs_to method isn't being inherited by one of the subclass. Am I missing something obvious?

class B < A
  belongs_to :x
  belongs_to :y
end

class A < ApplicationRecord
  belongs_to :z
end

Error:
D#test_should_get_new:
ActionView::Template::Error: undefined method `belongs_to' for B:Class
    app/models/B.rb:2:in `<class:QcQpcrSampleTest>'
    app/models/B.rb:1:in `<top (required)>'
    app/views/D/_form.erb:45:in `block (2 levels) in _app_views_D__form_erb___2546218964114172504_70133388141640'
    app/views/D/_form.erb:22:in `block in ...
    app/controllers/application_controller.rb:78:in `set_time_zone'
    test/controllers/D_test.rb:12:in `block in <class:DTest>'
Brian H
  • 1,033
  • 2
  • 9
  • 28
  • Did you create application_record.rb in app/models? – Vasilisa Feb 19 '19 at 15:22
  • @Vasilisa No, is this necessary? ApplicationRecord is just the rails 5 replacement for ActiveRecord https://stackoverflow.com/questions/37359527/why-rails-5-uses-applicationrecord-instead-of-activerecordbase – Brian H Feb 19 '19 at 16:06

1 Answers1

0

You're inheriting class A from ApplicationRecord, but your app have no idea - what is it. Create application_record.rb in app/models and place code inside it:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

It's OK now, all your models inherit from ActiveRecord::Base and have all association methods

Vasilisa
  • 4,604
  • 3
  • 20
  • 25