sql - Select unique records and display as category headers in rails -
i have rails 3.2 app running on postgresql, , have data want display in view, stored in database in structure:
+----+--------+------------------+--------------------+ | id | name | sched_start_date | task | +----+--------+------------------+--------------------+ | 1 | "ben" | 2013-03-01 | "check debris" | +----+--------+------------------+--------------------+ | 2 | "toby" | 2013-03-02 | "carry out y1.1" | +----+--------+------------------+--------------------+ | 3 | "toby" | 2013-03-03 | "check oil seals" | +----+--------+------------------+--------------------+
i display list of tasks each name, , names ordered asc
first sched_start_date
have, should ...
ben 2013-03-01 – check debris toby 2013-03-02 – carry out y1.1 2013-03-03 – check oil seals
the approach starting taking run query unique names , order them sched_start_date asc
, run query each name tasks.
to list of unique names, sql this.
select * ( select distinct on (name) name, sched_start_date tasks ) p order sched_start_date;
i know if correct approach (querying unique names running query tasks), or if there better rails way.
assuming have associations in model able run
@employees = employee.order(:name, :sched_start_date, :task).includes(:tasks)
you iterate on them:
@employees.each |employee| employee.name employee.tasks.each |task| task.name end end
this isn't gonna match needs, should show start.
Comments
Post a Comment