There is a model payment_request.rb
in our Rails 4.2 app and we would like to force to reload it with before_action
in its controller payment_requests_controller.rb
. Is there a way doing reloading?
@Mariah suggested you a way to reload an instance of a model class, but in case your intention was to really reload the definition of a class, you could do it with this trick:
before_action :reload_model
def reload_model
Object.send(:remove_const, :PaymentRequest)
load 'app/models/payment_request.rb'
end
Beware of side effects like impossibility to access a PaymentRequest
from other parts of same instance of your application during that class is reloading. Actually I doubt if doing this in your controller code is a right thing.
Reloading a class may be useful when some constant value should be updated (as it is filled upon first class loading and changed during time). But in case this situation occurs during your application is live, you'd better consider changing constant-based solution to something more appropriate.