Looking at someone else's code, not sure what this line does
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

What does this do in the Controller? Where would be a better place to put it?

# Line 10
def all
  @events = Event.all


Crowdsource coding tasks.

4 Solutions

Where is this code being used? It looks like it should be a scope or self method that lives in the model. I couldnt find anywhere where this method or variable was used.

I hypothesize that the def all; @events = Event.all; end does the same thing as .all in the Rails API. I'm probably looking for someone to corroborate the hypothesis and explain if doing a def all; @events = Event.where(...); end in the controller would override the return value of the default .all
akshatpradhan 6 years ago
Right... it looks that way but without going through all the code, there is no way to be 100% sure.
skram 6 years ago
Winning solution

.all will return All of the ActiveRecord results, it is a rails scope that is automatically there for you.

Does doing a `def all; @events = Event.all; end in the controller override the .all default rails scope?
akshatpradhan 6 years ago
Even if this were the case, the intended logic does the same thing.
michaelminter 6 years ago
No it doesn't override the default rails scope, all def all; end does is create an end point in your controller called "all". So for example once you create a route for it, like get 'all', to: 'events#all' then you'll have this available: http://yoursite.com/events/all which will then run whatever query you have inside of the all action of your controller
magicmarkker 6 years ago
@magicmarkker is correct. If you want to "override" the all call in Rails you have to monkey patch Active Record or perform association in model.
michaelminter 6 years ago
@magicmarkker I think that's exactly the explanation I was looking for. Thank you!
akshatpradhan 6 years ago

Event.all, instructs Active Record to fetch the entire Event table in a single pass, build a model object per row, and then keep the entire array of model objects in memory.

But I guess that's where I'm confused. Why even specify def all; @events = Event.all; end if it seems to be doing what the default rails scope .all already does?
akshatpradhan 6 years ago
Likely it has purpose in the interest of developing an API. So http://campaign.dev/api/v1/event/all would return the intended results.
michaelminter 6 years ago
wait, that's how you make an api? do a def all; @events = Event.all...format.json; end and then you can curl http://campaign.dev/event/all.json?
akshatpradhan 6 years ago
There are a few different methods for creating RESTful-like APIs. But yes, that is one way, if you set up your routes as such.
michaelminter 6 years ago
@michaelminter Thank you! This answer opened up some ideas for me!
akshatpradhan 6 years ago

Defining a method in a controller in such a way (with an instance variable loading a collection) it usually indicates a controller action which is normally bound to a view through a route definition. In this case the all action doesn't have a view file neither a route defined, so it just seems to me an unused method/action.

Do you have other examples of this?

@kc00l The person who wrote the rails app is also learning rails. I was trying to practice reading & improving someone else's code. I think this is clearly code that should be deleted. It doesn't contain a test case and as you, @skram and others mentioned, there's no view or route for it. Perhaps the original author was also confused about an all action vs a .all scope. I'm going to reach out to him. Thank you!
akshatpradhan 6 years ago
View Timeline