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

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.

Thanks

1 Answer

Aaron Kaye
Aaron Kaye
10,948 Points

I believe you can do something like this:

StudioOrder.where(:name => "bla").price
Aaron Kaye
Aaron Kaye
10,948 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 :(