ruby on rails 4 - How to query ActiveRecord with having and group -
i have simple appointment model storing start_time , user_id. i'd appointments starting in next 7 days in ascending order , grouped day. ideally result hash or multidimensional array such as
{ '30 march' => [appt1,appt12, appt3], '31 march' => [appt5,appt2, appt7,appt8], '1 april' => [appt10,appt11,appt9, appt6,appt4], '2 april' => [appt15,appt21] }
the query below gets desired results not in format want.
appointment.order(start_time: :asc).where("start_time <= ?", 7.days.from_now.end_of_day)
is there way structure query or need format result separately?
one way group_by
appointments = appointment.order(start_time: :asc). where("start_time <= ?", 7.days.from_now.end_of_day). group_by { |a| a.start_time.to_date }
Comments
Post a Comment