Using Scopes to Write Custom Queries in Rails

July 15, 2014 by Mike Clark

In our Rails Level I course we look at how to define custom queries by writing class methods in models. For example, to find all events that occurred in the past (versus upcoming events), we write a past method in the Event model and then query past events using Event.past.

We build on that foundation in our Rails Level II course by learning how to write custom queries using the declarative style offered by the scope method. A scope simply names a chunk of query code, and behind the scenes it dynamically defines a class method that encapsulates the query code.

One of the keys to writing a scope is to make the query code a callable Ruby object (also called a Proc object). It's at this point that many folks are first exposed to a Ruby lambda, or the more cryptic -> shortcut. As a quick introduction to how lambdas work, check out this 3-minute excerpt from the Scopes module in our Rails Level II course where we convert the past class method to a scope:

If you found that helpful, go ahead and watch the entire Scopes module, as well as other free modules in the Rails Level II course. You can also check out the course introduction video for an overview of the application features we build in this course. Or jump right in and purchase the entire course (with downloadable HD videos!). Our course is based on Rails 4, so you'll learn and apply the most up-to-date techniques!

"I know after completing your Rails I and II courses that I can build pretty much whatever I want to in Rails. These two courses are far and away the best value-for-money Rails courses out there."

— Jonathan Mundy

Proof of Course Completion

June 24, 2014 by Nicole Clark

Want to share your accomplishment with friends, colleagues, your manager, or even prospective new employers? It's really easy. When you complete one of our online courses, it automatically shows up on your alumni page with the date of completion. For example, if you've completed all three online courses, your alumni page would look something like this:

Alumni Page

To view your alumni page, log in to your account page and click the "View Your Course Completions" link in the sidebar. You'll be taken to a page with a unique URL. Anyone you share this URL with can then admire your accomplishments! Note that if you don't have a username set in your account (it's blank by default), the URL includes your name and a few random numbers so it's not easily guessable. To personalize your alumni page URL, simply set a username in your account.

So go ahead, share your alumni page to let the world know that you're on top of your game!

What They're Saying About the Rails II Course

June 18, 2014 by Nicole Clark

Our Rails Level II course has been out a few months now and we're so very pleased to hear how it's helping folks learn and do more! Here are a few recent comments:

"I learned a ton in this course that I didn't learn from other sources online! I was immediately able to start putting together an application that has been stuck in my head for a long time."

Wassim Metallaoui

"I really liked the thorough explanations and learning WHY things work. I think that's missing in a lot of Rails tutorials out there. I feel grateful to have come across these courses since I was really spinning my wheels before, and now I feel like I can actually build something!"

Krista Fuentes

"This is the best Rails training hands-down! I've worked through several Rails courses and they don't even come close to explaining things as well as you do. You have the perfect balance of teaching the right amount of information such that I understood each new piece without any confusion."

Thomas Neal

"I know after completing your Rails I and II courses that I can build pretty much whatever I want in Rails. These two courses are far and away the best value-for-money Rails courses out there. If I could pick only one beginner and one advanced Rails resource to learn from, it would be your courses."

Jonathan Mundy

"I think we've all wasted time on silly tutorials and websites that promised quick and easy results but left us more confused than when we started. The awesome thing about this course is that...there's never that 'missing link' moment where you just think 'ok, they just skipped over something huge and now I don't understand what's happening'. The other huge benefit of this course is that you can come back to it as a resource. I would MUCH rather pay up front then spend money every month on a subscription. I'm confident that one day when I'm a very successful developer, I will look back to this series as 'the moment' that made it happen for me."

Hunter Stewart

More "A+," "pretty pumped," and "total awesome-sauce" comments ➝

Get started on your own journey from Ruby novice to Rails practitioner. Our three online courses — Ruby, Rails Level I, and Rails Level II — have everything you need. You'll be amazed at how much you learn, and can do. Try out the first few modules of each course for free today!

« Older Posts