maybe they are AR, because I get those values from a loop, where I issue
some sql: @temp_result = InvoicesCalcul.find_by_sql(‘SELECT sum(base) AS
"’+ @name_base + ‘" ,sum(import) AS "’ + @name_tax + ‘" ,sum(total_net)
AS "’ + @name_total + ‘" FROM invoices_calculs c, invoices i WHERE
c.tipus=’+KIND_TAX_IVA.to_s+’ AND c.invoice_id=i.id AND c.xcent=’+ @value +’ AND i.any=’+@c_year)
and I add those results into an array.
what I see, with your suggestion, I have the same issue as before, I
want to retrieve the values without knowing the attribute, only by the
index:
now I have to a[‘1600_base’] to get the value, I just want the value for
each index, not the attribute.
i’m going to deep into the rail’s book …
I’m still getting attributes and values, not a simple array …
But why do you put a trace at that point? Of course a is a hash there,
but you are not aggregating the hash, you’re collecting the collection of values of those hashes via a.values. You need to work
with tha variable “values” which is assigned the result of the
map.chain. Please inspect the array stored in the variable “values”
after that block of code has run.
I’m still getting attributes and values, not a simple array …
But why do you put a trace at that point? Of course a is a hash there,
but you are not aggregating the hash, you’re collecting the collection of values of those hashes via a.values. You need to work
with tha variable “values” which is assigned the result of the
map.chain. Please inspect the array stored in the variable “values”
after that block of code has run.
Now I see where was my error … I was using the variable a instead of
variable values.
Using the values gives me an array, but now I see that the order is not
the expected.
It’s the same as it was in the hash, so the problem is at the begining,
when I issue the sql:
@temp_result = InvoicesCalcul.find_by_sql('SELECT sum(base) AS
“’+ @name_base + '” ,sum(import) AS “’ + @name_tax + '” ,sum(total_net)
AS “’ + @name_total + '” FROM invoices_calculs c, invoices i WHERE
c.tipus=’+KIND_TAX_IVA.to_s+’ AND c.invoice_id=i.id AND c.xcent=’+ @value +’ AND i.any=’+@c_year)
with tha variable “values” which is assigned the result of the
when I issue the sql:
attributes:
base: “707.59”
Posted viahttp://www.ruby-forum.com/.
Hashes are unordered in ruby 1.8. The best way to guarantee you always
get the order you expect is to sort the keys. Using Xavier’s code from
above with “values” result sorted on hash key…
values = @arr[0].map do |ic|
ic.attributes
end.map do |a|
a.keys.sort.map do |k| # this part
a[k]
end
end.flatten
with tha variable “values” which is assigned the result of the
when I issue the sql:
attributes:
base: “707.59”
Posted viahttp://www.ruby-forum.com/.
Hashes are unordered in ruby 1.8. The best way to guarantee you always
get the order you expect is to sort the keys. Using Xavier’s code from
above with “values” result sorted on hash key…
values = @arr[0].map do |ic|
ic.attributes
end.map do |a|
a.keys.sort.map do |k| # this part
a[k]
end
end.flatten
Regards,
Jordan
thanks for the explanation, I see I have to learn more Ruby …
regards,
raimon
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.