0

I need to retrieve data from database column and put them to

[{1442507641,1},{1442507642,2},{1442507643,3},{1442507644,4}...]  

format as the plot format requires.

I'm trying to do this by :

@data = TableName.where(:id => requiredid)
  .map {|r| { r.received_date.to_i => r.value } }

but this returns format

data=[{1442507641=>6}, {1442507641=>7}, {1442507641=>5}, {1442507641=>6}, {1442507641=>5}, {1442507695=>9}, {1442507695=>9}, {1442507695=>7}, {1442507695=>8}]

How can I make the bracket as plot requires and remove the strange =&gt ?

1
  • you don't required key values ? comma separated hash Commented Sep 21, 2015 at 5:11

2 Answers 2

1

It seems like this ought to do what you're asking for:

parts = TableName.where(:id => requiredid).map do |r|
  sprintf("{%d,%d}", r.received_date, r.value)
end

@data = "[#{parts.join(",")}]"
Sign up to request clarification or add additional context in comments.

Comments

1

It's only for your options to manipulate your data:

@data = []
@data = User.where(:id => requiredid).map {|r| @data << "{#{r. received_date}, #{r.value}}"}

First you make @data as array. Than collect the string into array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.