Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,