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 Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Viewing Todo Lists

rigzin norboo
rigzin norboo
11,059 Points

Confused with scope :complete, -> { where("complete_at is not null") }

In incomplete scope it is clear that it will filter the rows from database that have complete_at: nil but in complete scope we are just passing string. How it is filtering the database.

1 Answer

Hi Rigzin,

The string is the database query; that's what gets passed into the query to deliver the required results. Using completed_at: nil is the Rails way of doing the same thing - Rails will convert completed_at: nil to a string such as "completed_ at is null" and query the database that way.

The two methods work the same way; one relies on Rails to generate the string, the other just uses the string directly. I would imagine that for more complicated queries, getting Rails to do the heavy lifting would make life easier, rather than trying to write the query directly.

I hope that helps,

Steve.