以下代码行引发错误,我不明白为什么。
fav_arr.push(Address.near([@address.latitude, @address.longitude], 5).where(addressable_type: 'User').pluck(:addressable_id))
ActiveRecord :: StatementInvalid:PG :: UndefinedColumn:ERROR:列 “距离”不存在
只要添加.pluck(:addressable_id),就会抛出距离列不存在的错误。
这个有效:
Address.near([@address.latitude, @address.longitude], 5).where(addressable_type: 'User').map{ |add| fav_arr << add.addressable_id }
这是SQL查询
Address.near([@address.latitude, @address.longitude], 5).where(addressable_type: 'User').to_sql :
: SELECT "addresses"."addressable_id" FROM "addresses" WHERE (addresses.latitude BETWEEN 45.42903640844458 AND 45.57376819155542 AND addresses.longitude BETWEEN -73.68277080563716 AND -73.47627419436283 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((45.5014023 - addresses.latitude) * PI() / 180 / 2), 2) + COS(45.5014023 * PI() / 180) * COS(addresses.latitude * PI() / 180) * POWER(SIN((-73.5795225 - addresses.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 5) AND "addresses"."addressable_type" = $1 ORDER BY distance ASC
感谢您的帮助
答案 0 :(得分:1)
请试试这个
Address.near([@address.latitude, @address.longitude], 5).where(addressable_type: 'User').reorder('').pluck(:addressable_id)
扩展@engineersmnky注释,near和pluck都会修改select语句。