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

时间:2019-02-19 15:04:15

标签: ruby-on-rails minitest

我最近升级到了rails 5.2,现在我在一个模型中遇到了错误,原因是子类之一没有继承Emirates_to方法。我缺少明显的东西吗?

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>'

1 个答案:

答案 0 :(得分:0)

您正在从ApplicationRecord继承类A,但是您的应用程序不知道-这是什么。在app / models中创建application_record.rb并将代码放置在其中:

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

现在可以,您的所有模型都继承自ActiveRecord :: Base并具有所有关联方法