I am trying to do a query for all stations , and
I have tried
< Strong> Additional information: Measures Remedy
I have read through it and there is only information about using a condition on Organizations.
The app only targets postgres, SQL is accepted.
edit: Added except for schema.rb:
create_table "measures", force: true do | T | T.integer "station_id" t.float "direction" t.float "max_wind_speed" t.float "min_wind_speed" t.float "temperature" t.datetime "created_at" "updated_at" t tatetime t.float "speed". Using float "speed_calibration" end add_index "comments", ["created_at"], name: "index_observations_on_created_at", using: BTREE add_index "comments", ["station_id"], name: "index_observations_on_station_id", using: BTREE create_table "stations", force: not true. T | "Name" t.string t.float t.float t.boolean "offline" t.string "time zone" t.integer "balance" "longitude" "hw_id" t.float "latitude" t.string t "user_id" . Datetime "created_at" t.datetime "updated_at" "slug" t.string "show" t.boolean, default: true t.float "speed_calibration", default: 1.0 t.datetime "last_observation_received_at" end
Additionally is this very code that is currently in use:
def all_with_latest_measure user_signed_in if? & Amp; & Amp; Current_user.has_role? (: Admin) station = station.all.load end stations || = Station. Where (show: true). Load if stations.size id = stations.map {| S | S.id}. Include (',') where = "WHERE m.station_id IN (# {ids})" until that ID Steps = Measure.find_by_sql (% Q {on DISTINCT (m.station_id, m.created_at) meter * measures meter # {where} m.created_at by order by DESC.}) Stations.each. Station | #Setup has many links between station and measurement # stop n + 1 queries solution = solution Remove {| M M.station_id == station.id} measure measure.station = station station.latest_measure = solution end end end
I believe in Rail 4 that you can apply a scope to the Association:
is near square stations: measure, -> {Order ('created_at DESC'). The limit (1)} end
then:
2.0.0-p353: 008 & gt; Select station "first" station station load (0.1 mms) "station". Select the "ID" ASC limit 1 measure load (0.1 mms) from the "stations" order of "stations" to "measure". * WHERE "measures" from "remedies" "Station_id" =? ORDER BY made_at DESC LIMIT 1 [["station_id", 1]]
Edit: In fact if you only need one of the most recent You has_one
. It will work for Rail 4 and Rail 3, with slightly modified syntax:
are the stations of the class: recently_mayere, - & gt; {Order ('created_at DESC')} CLASS_NAME: 'remedy' # rails 4 has_one: recent_measure, command: 'created_at DESC', CLASS_NAME: 'remedy' # rails 3 end
Comments
Post a Comment