2012-03-12 47 views
0

我在rails中使用了一些自定義查詢。 代碼片段看起來像從rails中獲取結果集對象的元數據

@time_spent = TimeEntry.find(:all, 
          :joins => "INNER JOIN sometable ON x = y", 
          :select =>"id, subject, spent_on") 

現在得到的值我使用

@time_spent[index][:spent_on] 
@time_spent[index][:subject] 

我想是到位的符號使用索引號。所以在運行時我不需要知道select子句中的字段。

爲例如我想要做類似這樣的

@time_spent[index][1] 
@time_spent[index][2] 

一些事情,如果我能得到結果集的元數據,我可以利用這些信息

評論嗎?

+0

擺脫的結果集字段名稱,使用attributes.k eys方法 – 2012-03-12 10:15:34

回答

0

當@time_spent是對象的集合,這將讓該屬性的值,該集合在第一[0]項中指定的索引:

@time_spent[0].attributes.values[index] 

因此,例如,獲得第5屬性的集合中的第二個對象的值:

@time_spent[1].attributes.values[4] 
0

擺脫的結果集字段名稱,使用方法attributes.keys