title: how to pluck or select values from a database and return separate arrays for each record in rails
body: <p>i have a model History(data:text) and the data is an serialized array</p>
  
  <p>I want to render the all of the saved histories as json; however calling the records forces them into 1 array when I need each history to be contained in a separate hash. </p>
  
  <p>When I call:</p>
  
  <blockquote>
    <p>History.limit(1).pluck(:data)</p>
  </blockquote>
  
  <p>everything is fine because I get 1 array:</p>
  
  <pre><code>[{"miners"=&gt;{},
    "stats"=&gt;
     {"amtDue"=&gt;422770422,
      "amtPaid"=&gt;0,
      "hash"=&gt;18,
      "identifier"=&gt;"global",
      "invalidShares"=&gt;1,
      "lastHash"=&gt;1515350526,
      "timeout"=&gt;1515350582003,
      "totalHashes"=&gt;7107814,
      "txnCount"=&gt;0,
      "validShares"=&gt;9598},
    "ticker"=&gt;
     {"base"=&gt;"XMR",
      "change"=&gt;"-5.01749443",
      "price"=&gt;"396.34459617",
      "target"=&gt;"USD",
      "volume"=&gt;"24647.97760706"},
    "timestamp"=&gt;1515350523}]
  </code></pre>
  
  <p>However, when I call 2 or more records they are contained in 1 array which won't work for my needs: </p>
  
  <blockquote>
    <p>History.limit(2).pluck(:data) </p>
  </blockquote>
  
  <pre><code>[{"miners"=&gt;{},
    "stats"=&gt;
     {"amtDue"=&gt;422770422,
      "amtPaid"=&gt;0,
      "hash"=&gt;18,
      "identifier"=&gt;"global",
      "invalidShares"=&gt;1,
      "lastHash"=&gt;1515350526,
      "timeout"=&gt;1515350582003,
      "totalHashes"=&gt;7107814,
      "txnCount"=&gt;0,
      "validShares"=&gt;9598},
    "ticker"=&gt;
     {"base"=&gt;"XMR",
      "change"=&gt;"-5.01749443",
      "price"=&gt;"396.34459617",
      "target"=&gt;"USD",
      "volume"=&gt;"24647.97760706"},
    "timestamp"=&gt;1515350523},
   {"miners"=&gt;{},
    "stats"=&gt;
     {"amtDue"=&gt;422770422,
      "amtPaid"=&gt;0,
      "hash"=&gt;4,
      "identifier"=&gt;"global",
      "invalidShares"=&gt;1,
      "lastHash"=&gt;1515353143,
      "timeout"=&gt;1515353179152,
      "totalHashes"=&gt;7125334,
      "txnCount"=&gt;0,
      "validShares"=&gt;9628},
    "ticker"=&gt;
     {"base"=&gt;"XMR",
      "change"=&gt;"3.74386788",
      "price"=&gt;"405.10595848",
      "target"=&gt;"USD",
      "volume"=&gt;"25292.54683583"},
    "timestamp"=&gt;1515353103}]
  </code></pre>
  
  <p>How do I get data for each history in a separate hash? Here's my controller, I want to render the arrays as json:</p>
  
  <pre><code>def inquiry
  @step = inquiry params[:step]
  @n = inquiry_params[:n]
  
  if @n.present?
    @history = History.limit(@n).pluck[:data]
    render :json =&gt; @history
  else
  ....
  end
  
   def inquiry_params
      accessible = [:step, :n]
      params.permit(accessible)
    end
  </code></pre>
  
  <p>edit:
  I'm now calling :</p>
  
  <blockquote>
    <p>@history = Hash[History.limit(@n).pluck(:data).flatten.map{ |el| [el] }].flatten</p>
  </blockquote>
  
  <p>I get the desired format; however there is an unwanted "null" that I don't understand where it comes from or how to get rid. </p>
  
  <pre><code>[{"miners"=&gt;["b9aeff80-e16d-4af6-b675-9f218eff8077"],
    "stats"=&gt;
     {"amtDue"=&gt;422770422,
      "amtPaid"=&gt;0,
      "hash"=&gt;28,
      "identifier"=&gt;"global",
      "invalidShares"=&gt;1,
      "lastHash"=&gt;1515435268,
      "timeout"=&gt;1515435354059,
      "totalHashes"=&gt;9370026,
      "txnCount"=&gt;0,
      "validShares"=&gt;11999},
    "ticker"=&gt;
     {"base"=&gt;"XMR",
      "change"=&gt;"1.18426262",
      "price"=&gt;"383.96558958",
      "target"=&gt;"USD",
      "volume"=&gt;"24605.93213606"},
    "timestamp"=&gt;1515435302},
   nil,
   {"miners"=&gt;["b9aeff80-e16d-4af6-b675-9f218eff8077"],
    "stats"=&gt;
     {"amtDue"=&gt;422770422,
      "amtPaid"=&gt;0,
      "hash"=&gt;28,
      "identifier"=&gt;"global",
      "invalidShares"=&gt;1,
      "lastHash"=&gt;1515435467,
      "timeout"=&gt;1515435505244,
      "totalHashes"=&gt;9374886,
      "txnCount"=&gt;0,
      "validShares"=&gt;12005},
    "ticker"=&gt;
     {"base"=&gt;"XMR",
      "change"=&gt;"0.52934940",
      "price"=&gt;"383.31067636",
      "target"=&gt;"USD",
      "volume"=&gt;"24591.95015563"},
    "timestamp"=&gt;1515435422},
   nil]
  </code></pre>