Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The other day I stumbled on Sandi Metz's rules, and one of them reads

When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done.

Still pretty new to Rails, but I always thought that my controller methods had some smell in them, and this confirmed it. I have a dashboard view for a parent model that displays information about their children (a different model) and the children's challenges(another model), all with different controllers. Here is an example of one of our controller methods.

def dash
  @parent = current_user
  @children = @parent.children
  @completed_challenges = @parent.assigned_challenges.where("parent_id =?", @parent.id).where("completed =?", true)
  @validated_challenges = @parent.assigned_challenges.where("parent_id =?", @parent.id).where("validated =?", true)
  @enabled_rewards = @parent.enabled_rewards.where("parent_id =?", @parent.id)
end

I was wondering if I could send multiple requests to get all of these objects from their respective controllers as opposed to lumping them all in one request. I know I can do this with Ajax, but is there a way just doing multiple http requests when the page is loading?

I appreciate the help!

share|improve this question
    
I'm not sure I'd stick too hard and fast to this for a dashboard view. The whole point of a dashboard is to show a bunch of data collectively in one spot. That said, you could instantiate the parent and use something like a decorator (via draper) to get the derived data. This keeps the controller and view both very clean. –  Chris Heald Apr 12 '13 at 22:58
    
Thanks @ChrisHeald, I finally had a chance to look up Draper/decorator. Definitely a pattern that can help me out in this situation. However, do you think that the type of data manipulation that I'm doing here is 'presentation-centric' enough (to quote the Draper README), or more something that would belong in the model? –  kmanzana Aug 2 '13 at 4:51

2 Answers 2

up vote 1 down vote accepted

The answer is NO.

AJAX was built specifically for that purpose, to overcome the short-comings of multiple http requests. Plus multiple http requests while page load is frowned upon because of the performance slump. It might seem lucrative path for a small project, but when it scales, you will definitely run into huge holes.

Though I am not a big fan of it, but following the beaten path can save you a lot of effort in this case. :)

share|improve this answer

When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done.

I see the usefulness of this rule, but I think you can interepret it in a slightly different way. When you load a controller, you do want it doing as little as possible. This is a given.

In your case, though, you seem to have a lot of different bits of data to load. Besides looking ugly, you'll see that the code also tends to get bigger and bigger as your dashboard supports more and more metrics. You don't really want to be in a situation where the number of lines of code increases proportionally with the amount of data an application shows.

Instead, send one object - a dictionary of all the data that you need to show. And I'd suggest moving the construction of that dictionary off to a model (or better still, a service) that builds it up (possibly based on configuration of some sort). The controller should just be fetching that object from somewhere and sending it back in the correct format.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.