简体   繁体   English

Rails 5.2继承类中的未定义方法belongs_to

[英]Rails 5.2 Undefined method belongs_to in inherited class

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. 我最近升级到了rails 5.2,现在在我的一个模型中遇到了错误,因为substance_to方法没有被子类之一继承。 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>'

You're inheriting class A from ApplicationRecord, but your app have no idea - what is it. 您正在从ApplicationRecord继承类A,但是您的应用程序不知道-这是什么。 Create application_record.rb in app/models and place code inside it: 在app / models中创建application_record.rb并将代码放置在其中:

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 现在可以,您所有的模型都继承自ActiveRecord :: Base并具有所有关联方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM