Can rail model methods be called from rack middleware?

def inside_rack_middleware MyModel.update_attributes(:ip => request.ip) redirect somewhere else end 

perhaps?

+4
source share
2 answers

got a response via @ supercoco9 on twitter, "if your middleware is in the rack / racks, you can use the models directly through AR"

+2
source

Yes, you can access your AR samples from your middleware. I added byebug to my middleware and was able to access my models.

EDIT: - So, I started writing Rack middleware for use in my Rails application. I tried to access my AR model in middleware, but its behavior was pretty funny. Everything will be fine for the first request filed after starting a new server. But any request that came after this led to an error: -

A copy of MyMiddleware has been removed from the module tree, but is still active

This error disappeared as soon as I stopped accessing the AR model from middleware. I just started with Ruby / Rails, so I don’t know specific terms in the world of Ruby / Rails / Rack. But in simple words, all I can say is that Rack middleware is a kind of plug and play library that anyone can use in any Rack application. Therefore, we should not depend on any Rails objects inside our middleware.

0
source

Source: https://habr.com/ru/post/1303785/


All Articles