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

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

Challenge does not pass with alternate syntax?

If I input the following in the challenge it does not pass

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

Has anyone got an idea why this is wrong? If I use the hash-method it works fine.

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

3 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

In the top code block you have an array with two values, in the bottom code block you have a hash with one value and a key that points to it. So it's not really alternate syntax as far as I can tell. What am I missing?

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

Yes it uses an array in the first and a hash in the second version but, as I understood it:

in the array condition ActiveRecord will take the second element "Hampton" and replace the question mark in the first element of the array with it, which is equivalent to saying find all instances where first_name is Hampton, i.e. first_name must equal "Hampton"

in the Hash condition we use the field we want to find as the key and set the value to the condition it needs to satisfy, essentially we are saying, apply whenever first_name is Hampton, i.e. first_name must equal "Hampton"

so while we can use the hash method, and yes an array is not a hash, isn't the logic behind both statements the same? so unless I am not overlooking a typo, shouldn't both statements pass?

with alternate syntax I mean a pure string, array and hash condition should all be possible in this case, or am I missing something?

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

From that it sounds like the Treehouse CC compiler is being picky again :^).

Maximiliane Quel
Maximiliane Quel
Courses Plus Student 55,489 Points

That is possible. Although I might be totally wrong or overlooking something. It has happened ... occasionally. So therefore I thought I'd ask :0)