ruby - Joining two arrays together to make json in rails -
i pretty ruby nuby i'm sure doing awful here. i'd join 2 arrays can pass controller json created. way can reasons of d3 library im using , requirements.
to have following in controller:
def self.including_relationships result=[] result['nodes']=user.pluck(:name,:group) result['links']=relationship.select('follower_id source, followed_id target, value').map{|x| [x.source, x.target, x.value]} result end
the controller:
def data @users = user.including_relationships respond_to |format| #format.html # index.html.erb format.json { render json: @users } end end
i end result, when passed through controller like:
{ "nodes":[ {"name":"myriel","group":1}, {"name":"napoleon","group":1}, {"name":"child2","group":10}, {"name":"brujon","group":4}, {"name":"mme.hucheloup","group":8} ], "links":[ {"source":1,"target":0,"value":1}, {"source":76,"target":62,"value":1}, {"source":76,"target":48,"value":1}, {"source":76,"target":58,"value":1} ] }
the table relationships correct (user relationship 1 many). think question of how can 2 queries combined format can render them in json in controller
to json in required format, need is, change array hash.
result = {}
and may map call below, if need array of hashes, links
map{|x| {source: x.source, target: x.target, value: x.value} }
Comments
Post a Comment