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.


How to query the active record for the following scenario

I have a model called StudioOrder where name and price are two attributes. Now I want to get price for a particular name. I am trying to do this .. like StudioOrder.find_by(name: "bla").price .. but it gives me no method error. What is the correct way to do it.


1 Answer

Aaron Kaye
Aaron Kaye
10,936 Points

I believe you can do something like this:

StudioOrder.where(:name => "bla").price
Aaron Kaye
Aaron Kaye
10,936 Points

Using a rails app I have and the rails console, I used a similar syntax as yours (only using my model) so I am thinking it is another issue. Does your schema for StudioOrder have a name field?

2.2.1 :016 > Registration.find_by(first_name: "Aaron").last_name
  Registration Load (0.5ms)  SELECT  "registrations".* FROM "registrations"  WHERE "registrations"."first_name" = 'Aaron' LIMIT 1
 => "Kaye" 

White spacing between brackets and find_by :(