标签: money-rails

Money-Rails Gem - 实例货币

我在我的rails 4应用程序中有一个模型称为资金.

我使用Money Rails处理货币/货币组件 - https://github.com/RubyMoney/money-rails

我的资金模型有3个资金属性叫做amount_expenses,amount_honorarium和amount_principal_financing.

资金模型还具有用于创建实例的用户的货币属性,以选择应该为三个资金属性中的每一个使用哪种货币.

当我运行迁移到add_monetize到三个资金属性中的每一个时,它创建了三个相应的货币属性.

我需要它们吗?我可以要求用户为每个实例选择一次货币,然后使用该货币保存三个供资属性吗?那会怎么样?如果我在资金表中只有一个货币属性,那么货币化是否会知道如何选择显示三种融资金额?

资金表有:

 t.boolean  "expenses"
    t.boolean  "honorarium"
    t.boolean  "financing"
    t.string   "currency"
    t.string   "size"
    t.integer  "amount_expenses"
    t.integer  "amount_honorarium"
    t.integer  "amount_principal_financing"
    t.float    "return_on_finance"
    t.integer  "period_of_return"
    t.text     "expense_description"
    t.integer  "scope_id"
    t.integer  "amount_expenses_pennies",             default: 0,     null: false
    t.string   "amount_expenses_currency",            default: "GBP", null: false
    t.integer  "amount_honorarium_pennies",           default: 0,     null: false
    t.string   "amount_honorarium_currency",          default: "GBP", null: false
    t.integer  "amount_principal_financing_pennies",  default: 0,     null: false
    t.string   "amount_principal_financing_currency", default: "GBP", null: false
Run Code Online (Sandbox Code Playgroud)

结束

谢谢

ruby-on-rails money-rails

6
推荐指数
1
解决办法
258
查看次数

使用money-rails:如何手动设置属性的货币

我目前正在使用:

  • 钱护栏 v0.12.0
  • 轨道 v4.1.6
  • 红宝石 v2.1.3

用例

我正在更新一个名为money type的属性dollars_per_month,我使用money-rails迁移助手(即add_money)创建了该属性.

所以,在我的模型中,我有:

monetize :dollars_per_month_cents, allow_nil: true
Run Code Online (Sandbox Code Playgroud)

我目前的架构有:

t.integer  "dollars_per_month_cents"
t.string   "dollars_per_month_currency", default: "CAD", null: false
Run Code Online (Sandbox Code Playgroud)

货币在config/initializers中全局设置:

config.default_currency = :cad
Run Code Online (Sandbox Code Playgroud)

我希望用户能够在文本字段中输入"500 USD"或"500"或"500 CAD",并正确保存.目前,当指定非全局货币时,它会抛出异常:

RuntimeError: Can't change readonly currency 'CAD' to 'USD' for field 'dollars_per_month'
Run Code Online (Sandbox Code Playgroud)

这是有道理的 - 我可以理解为什么这是自然强制执行的.

但是..我想强行改变货币而不关心这种强制执行,因为这个领域只是用来捕捉问题的答案.

简单的解决方案(尽管未来缺乏便利性)是将其转化为纯文本.但是,我想知道是否可以使用RubyMoney和/或money-rails执行此操作而不触及应用程序的其余部分.

笔记

  • 我尝试更改dollars_per_month_currency并保存模型,但dollars_per_month仍然返回基于CAD的Money对象.

  • 我在money-rails 自述文件中查看了Instance Currencies .虽然我相信我可以做到这一点,但我觉得它太过分了,因为它需要额外的货币领域......当我只想改变时dollars_per_month_currency.

欣赏任何人都必须分享的见解或经验!

ruby ruby-on-rails money-rails

5
推荐指数
1
解决办法
1872
查看次数

Money-Rails Gem:模型中的Humanized_money

在我的捐赠模型中,我已将amount_cents列货币化

我需要模型中捐赠金额的人性化版本(例如643.50),这样我就可以生成并向捐赠者发送PDF收据.

我尝试过humanized_money(self.amount)和self.amount.humanized_money,但是得到了错误:

NoMethodError: undefined method `humanized_money' for #<Donation:0x007fa687ebb678>
Run Code Online (Sandbox Code Playgroud)

如何在模型中获得这种人性化的形式?

class Donation < ActiveRecord::Base
  belongs_to :donatable, polymorphic: true
  belongs_to :added_by_user, foreign_key: "added_by", class_name: "User"

  store_accessor :details, :method

  monetize :amount_cents

  def donations_this_week(branch_id)
    sum =  Donation.sum(:amount_cents).to_money
    return humanized_money(sum)
  end

  def receipt
    Receipts::Receipt.new(
        id: id,
        product: "GoRails",
        message: "This receipt is to acknowledge that we have received a donation with the below details from #{self.donatable.name}",
        company: {
            name: self.donatable.branch.organization.name,
            address: "#{self.donatable.branch.address_line_1}\n#{self.donatable.branch.address_line_2}\n#{self.donatable.branch.email}\n#{self.donatable.branch.legal_details}",
            email: self.donatable.branch.email,
            logo: self.donatable.branch.organization.logo.url(:medium),
        },
        line_items: [
            ["Date",           created_at.to_s],
            ["Donor Name", self.donatable.name], …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rails-activerecord money-rails

5
推荐指数
2
解决办法
1537
查看次数

迁移到 Rails 6.1 后,connection_config 已弃用 RSpec 警告

我将一个应用程序升级到 Rails 6.1.0(从 6.0.3.3 开始,通过创建一个新的仅 api 应用程序,添加 RSpec,然后手动复制所需的文件)。

当我运行 RSpec 时,我看到以下警告:

DEPRECATION WARNING: connection_config is deprecated and will be removed from Rails 6.2 (Use 
connection_db_config instead) (called from <top (required)> at 
[...app/models/application_record.rb:1].
Run Code Online (Sandbox Code Playgroud)

我没有更改ApplicationRecord默认的类:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end
Run Code Online (Sandbox Code Playgroud)

我仅在运行 RSpec 时看到此警告。我没有在 Rails 控制台或 Rails 服务器日志中看到它。

这是我的config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV.fetch('DATABASE_HOST', 'localhost') %>
  username: <%= ENV.fetch('POSTGRES_USER', 'postgres') %>
  password: <%= ENV.fetch('POSTGRES_PASSWORD', '') %>
  database: <%= ENV.fetch('POSTGRES_DB', 'myapp_development') %>
  pool: …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails rspec-rails rails-activerecord money-rails

5
推荐指数
1
解决办法
4207
查看次数

money-rails,Money#==仅支持零数字

money-rails在Rails应用程序中使用了gem。到目前为止,它一直运行良好,但是最近我开始出现以下错误:

Money#== supports only zero numerics
Run Code Online (Sandbox Code Playgroud)

我不确定这是什么原因或如何解决。我最近跑步,bundle update所以我想有些东西已经更新。我的gemfile样子是这样的:

gem 'money-rails', '~>1'
gem 'eu_central_bank', "~>1.3.0"
Run Code Online (Sandbox Code Playgroud)

我的实现如下所示:

# model
monetize :price_in_cents

# fetch / converting currencies
eu_bank = EuCentralBank.new
Money.default_bank = eu_bank
eu_bank.update_rates
converted_price = eu_bank.exchange_with(Money.new(price_to_convert * 100, from_currency), to_currency)
Run Code Online (Sandbox Code Playgroud)

正如我之前提到的那样,这已经解决了,所以我不确定是什么在破坏它。

有任何想法吗?

更新资料

为了测试,我尝试了以下方法。

money = Money.new(100, from_currency)
Run Code Online (Sandbox Code Playgroud)

然后我得到了和以前一样的错误。但是,如果我尝试过:

money = Money.new(0, from_currency)
Run Code Online (Sandbox Code Playgroud)

它似乎有效。我觉得有点奇怪。

更新资料

这是我尝试保存记录时的回溯信息:

[“ /Users/[user]/.rvm/gems/ruby-2.5.1/gems/money-6.11.3/lib/money/money/arithmetic.rb:70:in!= =='", "/Users/[user]/.rvm/gems/ruby-2.5.1/gems/activemodel-5.1.6/lib/active_model/validations/numericality.rb:22:in'”,“ / Users / [user] /。rvm / gems / ruby​​-2.5.1 / gems / activemodel-5.1.6 / lib …

ruby currency ruby-on-rails money-rails

4
推荐指数
2
解决办法
612
查看次数

Money-Rails Gem:如何为所有货币制作选择列表?

我正在使用money-rails gem并希望在我的视图中显示不同货币的列表,但我现在的代码不起作用.

我有我的Price模型和领域in_centscurrency:

create_table :prices do |t|
  t.integer :in_cents, default: 0, null: false
  t.string :currency, default: 'USD', null: false
Run Code Online (Sandbox Code Playgroud)

现在根据Money gem和Money-Rails文档我必须做的事情如下:

class Price < ActiveRecord::Base

  monetize :in_cents, as: "amount", with_model_currency: :in_cents_currency

  def all_currencies(hash)
    hash.keys
  end
Run Code Online (Sandbox Code Playgroud)

比我的简单形式宝石的观点:

= f.input :currency, collection: all_currencies(Money::Currency.table)
= f.input :amount, required: false
Run Code Online (Sandbox Code Playgroud)

但这给了我错误:

undefined method `all_currencies' for #<#<Class:0xd154124>:0xd15bab4>
Run Code Online (Sandbox Code Playgroud)

为什么?

PS

我想显示ISO代码和名称United States Dollar (USD).

ruby ruby-on-rails ruby-on-rails-4 money-rails

3
推荐指数
3
解决办法
2980
查看次数

欧盟中央银行宝石重定向被禁止

目前我收到来自eu central bank gem on rails的错误消息;

2018-09-06T18:26:20.629896+00:00 app[web.1]:   Error ID: 79f8e4f3
2018-09-06T18:26:20.629903+00:00 app[web.1]:   Error details saved to: /tmp/passenger-error.ZqeFNI
2018-09-06T18:26:20.629905+00:00 app[web.1]:   Message from application: redirection forbidden: http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml -> https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml (RuntimeError)
2018-09-06T18:26:20.629907+00:00 app[web.1]:   /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/open-uri.rb:224:in `open_loop'
2018-09-06T18:26:20.629908+00:00 app[web.1]:   /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/open-uri.rb:150:in `open_uri'
2018-09-06T18:26:20.629912+00:00 app[web.1]:   /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/open-uri.rb:716:in `open'
2018-09-06T18:26:20.629916+00:00 app[web.1]:   /app/vendor/ruby-2.2.4/lib/ruby/2.2.0/open-uri.rb:34:in `open'
2018-09-06T18:26:20.629919+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.2.0/gems/eu_central_bank-0.5.0/lib/eu_central_bank.rb:87:in `doc'
2018-09-06T18:26:20.629941+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.2.0/gems/eu_central_bank-0.5.0/lib/eu_central_bank.rb:20:in `update_rates'
2018-09-06T18:26:20.629976+00:00 app[web.1]:   /app/config/initializers/money.rb:5:in `<top (required)>'
2018-09-06T18:26:20.630007+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
2018-09-06T18:26:20.630038+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
2018-09-06T18:26:20.630069+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
2018-09-06T18:26:20.630099+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
2018-09-06T18:26:20.630128+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:652:in `block …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails money-rails

3
推荐指数
1
解决办法
267
查看次数

如何防止Ruby钱浮点错误

我正在使用带有money-rails gem的Rails来处理钱列.

有没有办法防止出现浮点错误?(即使是黑客也会这样做,我只是想确保没有这样的错误呈现给最终用户)

Rspec示例案例:

  it "correctly manipulates simple money calculations" do
    # Money.infinite_precision = false or true i've tried both 
    start_val = Money.new("1000", "EUR")
    expect(start_val / 30 * 30).to eq start_val
  end
Run Code Online (Sandbox Code Playgroud)

结果

Failure/Error: expect(start_val / 30 * 30).to eq start_val

   expected: #<Money fractional:1000.0 currency:EUR>
        got: #<Money fractional:999.99999999999999999 currency:EUR>

   (compared using ==)

   Diff:
   @@ -1,2 +1,2 @@
   -#<Money fractional:1000.0 currency:EUR>
   +#<Money fractional:999.99999999999999999 currency:EUR>
Run Code Online (Sandbox Code Playgroud)

ruby currency ruby-on-rails money-rails

2
推荐指数
1
解决办法
693
查看次数

Rails Money-rails:如果使用迁移,是否需要货币化模型定义?

我对Money-rails的文档有点困惑,他们说如果你有一个名为“price_cents”的整数列,你只需要monetize :price_cents在模型中添加定义,但是他们也说你可以在数据库中添加 Money 字段迁移如:

def change
    add_money :products, :price
end
Run Code Online (Sandbox Code Playgroud)

但我的疑问是:如果我使用迁移助手,add_money那么我还需要monetize在模型中添加定义吗?或者仅当您有整数列时才需要?

ruby-on-rails-4 money-rails

2
推荐指数
1
解决办法
1451
查看次数

Money-Rails Gem和Instance Currencies

我试图在我的rails 4 app中使用money-rails gem.

我有一个参与者模型,其中包含每个货币和participant_cost的属性.我的目标是让用户为每个实例指定一种货币,以及参与费用的金额.

在我的参与模型中,我有:

货币化:participation_cost,with_model_currency :: amount_currency

在我的表单中,我要求用户选择货币并指定金额,如下所示:

<%= f.input :participation_cost, label: 'What amount will you pay for participation costs?', :label_html => { :class => 'question-participants' }, placeholder: 'Whole numbers only', :input_html => {:style=> 'width: 250px; margin-top: 20px',  class: 'response-participants'} %>

<br><br>
<%= f.input :currency, label: 'Select your costs currency', label_html: {class: 'fundingspace'}, collection: ["AUD Australian Dollars", "GBP British Pounds", "USD US Dollars" ], prompt: "Choose one" %> </div> <br>
Run Code Online (Sandbox Code Playgroud)

在我看来,我想显示货币和金额.我目前有一个字符串如下:

  <%=  "#{@project.scope.try(:participant).try(:participation_cost)} #{@project.scope.try(:participant).try(:currency)}" %>
Run Code Online (Sandbox Code Playgroud)

当我测试这个时,我只得到参与号码的数字.我没有得到货币.

在我的money.rb初始化程序中,我有:

 config.default_currency …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails money-rails

0
推荐指数
1
解决办法
646
查看次数