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 ActiveRecord Basics Query Interface Finding Scopes 2

Sarah A. Morrigan
Sarah A. Morrigan
14,329 Points

custom scope: incomprehensible code challenge

class Person < ActiveRecord::Base
  scope :hamptons, -> { where ("first_name = 'Hampton'") } 
end

gives an error saying "you are not looking to see if Person's first_name is Hampton"

but that is precisely what I seem to be supposed to be doing when scoping first_name is Hampton.

class Person < ActiveRecord::Base
  scope :hamptons, -> { where ("first_name LIKE ?",  "Hampton") } 
end

did not do any good either.

I am not sure what this is all about.

person.rb
class Person < ActiveRecord::Base
  scope :hamptons, -> { where ("first_name = 'Hampton'") } 
end

# Having trouble?
# Rails has fantastic documentation! Here's the link about scopes:
#
# - http://guides.rubyonrails.org/active_record_querying.html#scopes
#

3 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

Try

scope :hamptons, -> { where(first_name:  "Hampton") } 

The issue with this code:

scope :hamptons, -> { where ("first_name = 'Hampton'") } 

Is that you've wrapped the whole condition in quotes which makes it all a string. You need the key, first_name: and then the name "Hampton" as a string.

Sarah A. Morrigan
Sarah A. Morrigan
14,329 Points

OK, I did just that and removed the quotes around the condition.

So now it looks like

class Person < ActiveRecord::Base
  scope :hamptons, -> { where (first_name = "Hampton") }
end

I still get the same error message "You're not checking to see if the 'Person's' 'first_name' is 'Hampton'."

:(

EDIT: I realize that I wrote first_name= above instead of first_name:

I changed it to

class Person < ActiveRecord::Base
  scope :hamptons, -> { where (first_name: "Hampton") }
end

And now it gives me an unspecified error ("Bummer! Try again") instead.

Brandon Barrette
Brandon Barrette
20,485 Points

No space between where and the ()

These quizzes need to have the flexibility so that if you write the CORRECT code but in a different way (without SQL injection it will work.

scope :hamptons, -> { where("last_name = ?", "Hampton")}

is a perfectly acceptable scope - and it keeps telling me it's wrong - with no direction how - that's just awful teaching...it's not that hard to create an array of acceptable answers - heck you taught us how.