I have a postgres db with each row representing an "index" (just a name not the keyword). For each market, there is an attribute "stats" thats a two dimensional array of numbers that I put into the db from an external source.
create_table "indices", force: true do |t|
t.string "name", null: false
t.float "returns", default: [], array: true
t.float "navs", default: [], array: true
t.float "stats", default: [], array: true
t.string "updated_at"
t.string "created_at"
t.integer "idx_type_id"
end
My problem is how do I get the stats values out of the db and into my rails program?
In the console, I tried the following code:
a = Index.find(1)
a.name
a.stats
It correctly gets the name attribute, but I get the following error for the stats attribute
undefined method `to_f' for ["0.259854651130407", "0.261425018884344"]:Array
The numbers shown inside the square brackets are the values of the first row of the 2D array. Any ideas?
EDIT: I'm running rails 4.0.1 and here's the original migration to create the table. Is there a syntax error in here or something?
class CreateIndices < ActiveRecord::Migration
def change
create_table :indices, {:id => false} do |t|
t.string :name
t.float :returns, array: true, default: []
t.float :navs, array: true, default: []
t.float :sharpe
t.timestamps
end
execute "ALTER TABLE indices ADD PRIMARY KEY (name);"
end
end
EDIT 2: Here is the output from the following query in psql
query:
select stats from indices where id < 3
output:
stats
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------
{{0.259854651130407,0.261425018884344},{0.114409534648231,0.113829156756227},{2.27126744225792,2.28284789710692},{0.0562802038101342,0.0561701509132937},{4.61715902819129,4.65416265816855},{0.0
92128059492271,0.0921734852013666},{2.82058096699852,2.81919090465893},{0.0292770008925915,0.0291771074372238},{8.87572644765545,8.95993612275702}}
{{0.259854651130407,0.261425018884344},{0.114409534648231,0.113829156756227},{2.27126744225792,2.28284789710692},{0.0562802038101342,0.0561701509132937},{4.61715902819129,4.65416265816855},{0.0
92128059492271,0.0921734852013666},{2.82058096699852,2.81919090465893},{0.0292770008925915,0.0291771074372238},{8.87572644765545,8.95993612275702}}
(2 rows)
\d indices
from withinpsql
. – mu is too short Dec 18 '13 at 22:13[[0.25, 0.26]]
than[0.25, 0.26]
? Maybe including some of theselect stats from indices
data frompsql
would help clarify things. My current guess is that Rails4 only understands one dimensional arrays. – mu is too short Dec 18 '13 at 22:24