English 中文(简体)
把钱物转化成铁路总线3.1
原标题:Adding a Money object transformation to a sum query in Rails 3.1

似乎同样如此的另一种看法是:

由于这里得到的帮助,我很容易得到一些选定的交易:

@trip_hash = transactions.sum(:amount_cents, :group => :trip_id)

然而,问题是:amount_cents一栏是一个原始货币目标,需要先加以修改,以适应货币兑换。 货币“与”计划类似:

composed_of :amount, 
          :class_name => "Money", 
          :mapping => [%w(amount_cents cents), %w(currency currency_as_string)], 
          :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }, 
          :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can t convert #{value.class} to Money") }

我可以轻易地呼吁:

transactions.map(&:amount).inject(:+)

要获得一个经过转变的孙子,但我可以说明如何在集团中这样做。

许多人再次感谢帮助!

最佳回答

大量 can和读物,但最后数字如下:

trip_hash = bankroll.transactions.group_by(&:trip_id).map {|tr,t| Hash[tr, t.map(&:amount).inject(:+)]}

=>[{0=>#<Money cents:137693 currency:USD>}, {7=>#<Money cents:-39509 currency:USD>}, {10=>#<Money cents:50009 currency:USD>}]

地图中地图确实如此! 通知使人认为是友好的,为了格式的目的保留货币标的。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签