Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Ruby on Rails Questions

Ruby / Rails Coding

A class contains the following method:

def self.get_sold_vehicles
  Vehicle.where(sold: true)
end

What is the purpose of self in this method definition?

Give an example of calling this function?

The following URL is entered in a browser, pointing to an application built in Rails:

http://localhost:3000/users/myaction?name=fred&age=10

a) What controller will handle this request?

b) What is the action in the controller that will be called?

c) In the controller, what will the function call params[:name] return?

2 Answers

Tim Knight
Tim Knight
28,888 Points

Hi Ivan,

Are these questions part of a Treehouse challenge?

Kevin Korte
Kevin Korte
28,149 Points

self refers to the class this method is defined in. I would image this method is inside a Vehicles class, and so self points back to that. This code effectively creates a class method on the class it's defined in, through self.

To answer your other questions

  1. Impossible to know because it can be routed anywhere, and must be routed in your routes.rb file. I would make logical sense that this would go to the users controller - that would be the convention anyway.

  2. Also impossible to know for the same reason, but again rails convention tells us this should go to the myaction method inside of the User class. But that's up to the developer to route in the routes.rb file

  3. That should return whatever is in the name parameter, in this case "fred". It'll actually probably return

{ "name" => "fred" }