简体   繁体   English

覆盖Rails 4中的现有模型

[英]Overriding existing model in Rails 4

I am trying to override an existing active record model but the new active record model doesn't have methods from the old active record model. 我试图覆盖现有的活动记录模型,但是新的活动记录模型没有旧的活动记录模型中的方法。 Here is the code that I am trying to use 这是我尝试使用的代码

class ModelA < ActiveRecord::Base

   def method_modela_1
     logger.info "I am in method_modela_1"
   end

   def method_modelb_2
      logger.info " I am in method_modelb_2"
   end
end

Next I override the ModelA with ModelB 接下来,我用ModelB覆盖ModelA

class ModelB < ModelA
   def foo
      logger.info "foo method from model B"
   end
end

So now I am trying to write a code that is trying to access find_or_create_by using the following code 所以现在我试图编写一个代码,尝试使用以下代码访问find_or_create_

some_variable = ModelB.find_or_create_by(:id => 1234)

but it says ***** NoMethodError Exception: undefined method 'find_or_create_by' for ModelB:Module** 但是它说***** NoMethodError Exception: undefined method 'find_or_create_by' for ModelB:Module**

What am I missing? 我想念什么? For record I am using 'rails', '4.2' 为了记录,我使用的是'rails', '4.2'

The keyword is "NoMethodError Exception: undefined method 'find_or_create_by' for ModelB: Module ". 关键字为“ NoMethodError异常:ModelB: 模块的未定义方法'find_or_create_by”。

You have a module ModelB somewhere and, due to load order and/or constant lookup rules, it shadows your model. 您在某处有一个module ModelB ,由于加载顺序和/或恒定的查找规则,它使您的模型module ModelB了阴影。

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

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