简体   繁体   中英

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

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

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

but it says ***** NoMethodError Exception: undefined method 'find_or_create_by' for ModelB:Module**

What am I missing? For record I am using 'rails', '4.2'

The keyword is "NoMethodError Exception: undefined method 'find_or_create_by' for ModelB: Module ".

You have a module ModelB somewhere and, due to load order and/or constant lookup rules, it shadows your model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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