Reputation: 2440
I have the following code in my rails controller:
@main_mastertest = $connection.execute("SELECT * FROM builds;")
@l2_tmp_mastertest = Array.new
@l2_tmp_mastertest = @main_mastertest.map {|y|[y[0]]}
@l2_mastertest = Array.new
@l2_mastertest = @l2_tmp_mastertest.inject(Hash.new(0)) { |hash,element|
hash[element] +=1
hash }
@l2_mastertest = @l2_mastertest.sort_by { |x, _| x }.reverse
After that I try to do something like this in my view :
<% @l2_mastertest.each_with_index do |row1, index1| %>
<% @l2_mastertest.each_with_index do |row2, index2| %>
<% if row2[0][ /(\d+\.\d+)/ ].to_s == row1[0].to_s %> # LINE X
..................
<% end %>
<% end %>
<% end %>
But it gives me an error on line X saying : can't convert Regexp into Integer
Upvotes: 0
Views: 375
Reputation: 27207
If I simulate what I think is going on with the data structures in your question, I get this:
@l2_mastertest = { '3.1.4' => 7, '1.2.3' => 8 }
=> {"3.1.4"=>7, "1.2.3"=>8}
@l2_mastertest.each_with_index { |row2,index| p row2,index }
["3.1.4", 7]
0
["1.2.3", 8]
1
So a structure like row2[0][ /(\d+\.\d+)/ ]
is the same as e.g. "3.1.4"[ /(\d+\.\d+)/ ]
Edit: Removed my previous "answer", because actually "3.1.4"[ /(\d+\.\d+)/ ] => "3.1"
, which is fine in Ruby (and I learned something today :-). Something else (possibly in code not shown) is making the @l2_mastertest hash not behave as expected.
Potentially this is a database/model issue, and as commenters have suggested, row[0]
does not contain a string.
I would suggest instead that SELECT * FROM builds;
is risky, since you are relying on database to return columns in particular order. You should change it to fetch the column data you need, in order e.g.
SELECT version, result, build_id FROM builds;
so that you are certain that later processing is working with the column that you think it is. Simply re-building or transferring the database could change the order that the RDBMS returns the columns in a SELECT *
and make previously-working code break.
Upvotes: 1