ruby - Create array of arrays from database query in rails -


ive got table consists of 2 fields called follower_id , followed_id. need create query creates array of each row , puts in overall array end structure looks like:

"edges": [     ["1", "2"],     ["1", "3"],     ["3", "4"],     ["3", "5"]   ] 

so far have

 def self.including_relationships   result={}   result["edges"] relationship.all.each |relationship|     result[""]= relationship.select(:follower_id.to_s,:follower_id.to_s)   #the code here called once each user   # user accessible 'user' variable end   result end 

but produces:

 edges: [ "[4, 3, 3, 4]", "[3, 4, 3, 4]" ] 

you can use map build array like:

relationship.all.map { |r| [r.follower_id.to_s, r.followed_id.to_s] }  

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -