samedi 1 décembre 2018

How do I find all the records that have a specific value of an association?

I have a property model that belongs_to :property_type.

I would like to create a scope on my Property model that finds all the records of a specific property_type.

How do I find all the property records with property_type.name == "Residential"?

I have tried the following:

> Property.where('property_type.name = "Residential"').count
   (5.4ms)  SELECT COUNT(*) FROM "properties" WHERE (property_type.name = "Residential")
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "property_type"
LINE 1: SELECT COUNT(*) FROM "properties"  WHERE (property_type.name...

Also this:

> Property.where('property_types.name = "Residential"').count
   (1.8ms)  SELECT COUNT(*) FROM "properties" WHERE (property_types.name = "Residential")
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "property_types"
LINE 1: SELECT COUNT(*) FROM "properties"  WHERE (property_types.nam...

Aucun commentaire:

Enregistrer un commentaire