Eu tenho um property
modelo que belongs_to :property_type
.
Gostaria de criar um escopo no meu Property
modelo que encontre todos os registros de um específico property_type
.
Como encontro todos os property
registros com property_type.name == "Residential"
?
Eu tentei o seguinte:
> 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...
Também este:
> 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...
Você pode usar associações para filtrar por property_type
Property.joins(:property_type).where(property_types: {name: "Residential"}).count
OU
Property.joins(:property_type).where("property_types.name = ?", "Residential").count
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras