ruby on rails - JBuilder loop that produces hash -
i need loop produces hash, not array of objects. have this:
json.service_issues @service.issues |issue| json.set! issue.id, issue.name end
that results:
service_issues: [ { 3: "not delivered" }, { 6: "broken item" }, { 1: "bad color" }, { 41: "delivery problem" } ]
i need this:
service_issues: { 3: "not delivered", 6: "broken item", 1: "bad color", 41: "delivery problem" }
is possible without converting ar result hash manually?
jbuilder dev here.
short answer: yes. it's possible without converting array of models hash.
json.service_issues @service.issues.each{ |issue| json.set! issue.id, issue.name } end
but it'd easier prepare hash before-hand.
json.service_issues hash[@service.issues.map{ |issue| [ issue.id, issue.name ] }]
Comments
Post a Comment