I have an images table and a locations table I want to retrieve a list of images that are the latest images for each location within a certain boundaries.
SELECT * FROM images
WHERE location_id IN
(SELECT id FROM locations
WHERE latitude > 17.954 AND latitude < 52.574
AND longitude > -107.392 AND longitude < -64.853)
This is with a nested query, but we could achieve the same with a join. This works if we want all images for each location, but I would like to get only 1 image per location (the most recent)
Here are the main fields of these tables
table "images"
integer "id"
text "image_name"
text "caption"
integer "location_id"
datetime "created_at"
datetime "updated_at"
integer "view_count"
table "locations"
integer "id"
text "name"
float "longitude"
float "latitude"
datetime "created_at"
datetime "updated_at"
string "city"
string "address"
string "province"
string "country"
string "post_code"
any idea?
Bonus points if there is a way to do this using rails activerecord API