3 ms·
This encourages having SQL queries initiated by the view, after the header has rendered. This seems antithetical to MVC to me.
by aaronblohowiak 16y ago
This encourages having SQL queries initiated by the view, after the header has rendered. This seems antithetical to MVC to me.
- jtgeibel 16y agoIn some cases the query is lazy loaded with a call to .each or something similar in the view already. In other cases, the query is run in the controller before anything is rendered. I think the main goal is to improve client side performance (by downloading scripts and other referenced files sooner) and see the most benefit on pages that load multiple models, for instance a sidebar showing popular posts and recent activity.
- aaronblohowiak 16y agolazy loading is bad for overall performance, though. Retrieving all of the models with eager-loading will avoid exploding query counts and lower the total time to render a page (and lower the load on your servers.)
- dasil003 16y agoHe's not talking about eager loading associations. It's AR 3.0 backed by arel that allows ALL queries to be lazily loaded, as in, they are declared in the controller cleanly, but they only run in the view (and furthermore they only run if the view needs them). Including assocations is an orthogonal issue. As far as I know they get the same benefit out of the box in Rails 3. For example, I think you can say: @posts = Post.where(:published => true).include(:comments) And it will still load it all eagerly, but it won't do it until you actually iterate over @posts.
- aaronblohowiak 16y agoAh, yes. I see what you are saying now. I was unaware that the abstract relational algebra was also able to defer joins. That is really an impressive bit of kit. While this does satisfy the objection to association loading, you still have the general problem of delaying the flushing until after all controller processing has completed. for nontrivial applications, this may indeed take quite some time (talking with disparate backends for a SOA, for example.)
- dasil003 16y agoWell what would you prefer? A complete re-architecture of Rails to support SOAs? Declaring head and body renders separately? They did the hard work for ActiveRecord. Most Rails apps still use ActiveRecord and have a relatively straightforward architecture. If you want the same benefit for your SOA architecture, it's not that hard, just build a middleware layer that defers the queries like arel does. Even better, follow the facebook approach, render a page shell and load the content via AJAX. That will ultimately get you the best performance by far, allowing faster response times, piecemeal loading, maximal offloading of processing to the client side, and opens up interesting avenues for caching possibilities.
- aaronblohowiak 16y agoThe ability (though of course not the requirement) to declare head and body renders separately would allow you to return the header before your processing, regardless of wether that processing is more intensive in the controller or the main body view. Deferring only works if having a stub of a request is sufficient to proceed to the next step in the execution path. Unless you are going to implement your own conditionals (which, admittedly, is doable in ruby,) then you are going to force the evaluation of the request as soon as you want to use it to make a decision.
- collint 16y agoNot really, in Rails, you might have this controller code: @things = Thing.where(:it => "good") And this view code: <% for thing in @things %> <%= thing.name %> <% end %> But the SQL query doesn't fire in the controller. It gets kicked in the view when you "for x in y" Concerns still wonderfully separated.
- aaronblohowiak 16y agoNot really, in Rails, you might have to do more than just retrieve some models. For instance, you might have to load up the current user, grab some stuff from memcache, check with your SSO system to validate the session, and then retrieve the data pertinent to the current request. Then, you might have to make some data modifications (which will create transactions and hit your db.) Finally, the view rendering can begin. In only the trivial cases can you defer the actual SQL queries from being performed before the view is rendered.
- dasil003 16y agoI don't get your point. Just because you may have to do some things in a before_filter or whatever doesn't mean you need to do all things there. As far as accessing stuff out of memcached is concerned, there's no reason that can't also be deferred to the view. Claiming that lazy loaded queries is only a benefit for "trival cases" is a strawman. It's a hugely powerful functionality for ActiveRecord that you can utilize in many ways, and would be very hard to implement without low level support. Cached attributes can often easily be made available via concise single model methods that operate transparently without the controller OR the view needing to know they are cache-backed. Plus, even if you are loading stuff out of memcached in the controller, it's going to be fast, because that's the whole point of memcached. ActiveRecord meanwhile, normally takes a huge percentage of rendering time. Being able to defer those queries while still allowing the controller to declare them is actually a huge combination of performance flexibility and separation of concerns. Previously, if you wanted to defer them "cleanly", you'd have to create model methods, but even there you would have to pass params through somehow or generally do something uglier than what you have to do now.
- fizx 16y agoThere's other schools besides MVC, including "component-oriented."