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 ...
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.
@temp_result is already an array of ARs.
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:
No, no. Please reread the code, it extracts the values without touching the keys. I think that's what you want.
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.
-- fxn
···
On Dec 7, 2007, at 12:13 AM, Raimon Fs wrote:
values = @arr[0].map do |ic|
ic.attributes
end.map do |a|
# a.values
debug(a)
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)
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
···
On Dec 7, 2:06 am, Raimon Fs <co...@montx.com> wrote:
Xavier Noria wrote:
> On Dec 7, 2007, at 12:13 AM, Raimon Fs wrote:
>> 1600_tax: "13210.85"
>> 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)
I suppose, that @temp_result should be:
---
- !ruby/object:InvoicesCalcul
attributes:
base: "707.59"
tax: "0.0"
total: "707.59"
On Dec 7, 2:06 am, Raimon Fs <co...@montx.com> wrote:
> 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 ...