0
我有两种模式:学校和地址。根据关联模型中的值排序记录
Address
has_many :schools
School
belongs_to :address
t.bigint "address_id"
现在我需要在学校模型中构建范围,以便根据地址表中的城市发生情况对学校进行排序。所以,我需要以下结果:
School_1 | City_most_popular
School_2 | City_most_popular
School_3 | City_most_popular
School_4 | City_most_popular
School_5 | City_quite_popular
School_6 | City_quite_popular
School_7 | City_not_popular
我写了这个:School.joins(:地址)。集团( “schools.id,addresses.city”)顺序( “addresses.city ASC”),但这只。允许按城市分组记录并忽略发生。
不幸的是没有在Postgresql上工作。在分组条款中需要schools.id。我添加了这个,但查询结果是错误的。 – user2942937
已更新的答案。 @ user2942937 – ninjarails