i have library defines base class other classes derive from. rails, on initialization, should provide of classes library. rails, however, doesn't recognize class methods inherited library class. here example:
model: app/models/mailing_address.rb:
require 'account_model' class mailingaddress < accountmodel # class accessors, initializer , method end library class: lib/account_model.rb
################################### # accountmodel # # super class inherited # domain classes of application. # responsible api calls , # error handling. ## class accountmodel class << self def get_all id = nil # class method implementation end ## # id # fetch method, parses url provided child class in path hash # , performs appropriate fetch # method returns class instance set fetch def id # class method implementation end def update_existing options # class method implementation end def create_new options #class method implementation end def delete_existing id # class method implementation end end these methods wrap api calls using httparty gem. on load, methods get , get_all recognized. however, create_new, , update_existing not:
app/controllers/mailing_address_controller.rb:
def update @mailing_address = mailingaddress.update_existing params[:mailing_address] . . . end throws following error:
processing mailingaddressescontroller#update json . . . completed 500 internal server error in 133ms nomethoderror (undefined method `update_existing' mailingaddress:class): app/controllers/mailing_addresses_controller.rb:17:in `update in passenger, need reload tmp/restart.txt, in webrick, need restart server.
i not see behavior in irb.
here i've tried far no success:
- adding initializer file in config/initializers
- adding require statements (as in example) in each model class
- wrapping library class in module
- renaming troubling methods (update_existing => update, foo, etc)
i have never seen behavior in rails app before, have library class works fine.
i'm running: - ruby-2.1.1 - rails 4.03 - passenger 5.07
update:
while attempting investigate further, uncovered yet issue:
i added class method mailingaddress:
class mailingaddress < accountmodel . . . def self.debug_methods return self.methods end which throws "methodnotfound" exception. since does work in rails console, not in webrick or passenger, i'm tempted there server caching going on.
update
after shutting down , restarting, situation reversed:
-webrick processes request -passenger processess request successfull -rails console throws error:
webrick , passenger:
processing mailingaddressescontroller#update json . . . completed 200 ok console:
mailingaddress.update_existing params nomethoderror: undefined method `update_existing' mailingaddress:class i'm guessing it's first come first serve whomever gets loaded class.
config.autoload_paths set correctly:
config.autoload_paths += %w(#{config.root}/lib) last update workaround seems work clobbering tmp/ directory , restarting (passenger need have touch tmp/restart.txt ran).
this, however, crappy workaround. bug!
you have call method on instance of class:
mailingaddress.new.update_existing params[:mailing_address] or
@mailing_address.update_existing params[:mailing_address] if don't need update_existing instance method can move outside of self block.
also, might consider putting account_model.rb file in app/models. way won't have require @ top of mailing_address.rb
Comments
Post a Comment