kmanzana
kmanzana

Reputation: 1198

Multiple Rails controller requests on one page

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!

Upvotes: 1

Views: 989

Answers (3)

Andy Markham
Andy Markham

Reputation: 11

The recommended approach is using the Facade Pattern. A perfect example of how to handle this is provided by Thoughtbot in the "Only instantiate one object in the controller" section of this blog post:

http://robots.thoughtbot.com/sandi-metz-rules-for-developers

Upvotes: 1

Sudhir Jonathan
Sudhir Jonathan

Reputation: 17516

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.

Upvotes: 0

banjoSas
banjoSas

Reputation: 194

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. :)

Upvotes: 1

Related Questions