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
Post a Comment