I have a RoR app that does a join between multiple tables and returns the resultset as json back to the client.
I need help in figuring out how to send unique column values only.
For e.g., if you have an employee and department table, doing a join on the two would return:
Physics John
Physics Joe
Math Charlie
Math Sheila
How can I return data back to the client that removes duplicate values, for e.g.
Physics => { John, Joe }
Math = > { charlie, Sheila }
I want to reduce unnecessary sending of duplicate data over the wire.
Any thoughts?
EDIT:
Changed it to this (found it on stack overflow) and it worked:
@lec_hash = @lectures.inject({}) do | result, row |
result[row.subject] = [] if result[row.subject].nil?
result[row.subject] << row.name
result
end
Prepare a hash where key is your subject name and value is array of names opted the subject
Assumptions :
resultis array of AR records and model hassubjectandname(person’s name) attribute(column)and then convert
hash.to_json