货币宝石:从父模型继承货币
有人可以告诉我如何从父模型继承货币吗?
我使用 Money gem (https://github.com/RubyMoney/money) 并有 2 个模型 (市场和贸易限制)。
市场价格会定期更新,如果存在具有相同价格的交易限额,则会通知用户。
因为市场已经存储了货币,所以我不想在 tradelimit 模型中再次存储它(不干):
class Market << AR
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_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") }
...
end
#tradelimit.rb
class Tradelimit << AR
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_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") }
...
end
我现在如何从父(市场)模型继承货币? 我尝试覆盖 tradelimit 模型中的货币方法,但这不起作用..
有什么建议吗?谢谢!
编辑:
或者有没有办法(没有composed_of功能)来提供此功能?
Can someone tell me how to inherit the currency from a parent model?
I use the money gem (https://github.com/RubyMoney/money) and have 2 models (market and tradelimit).
The price of the market becomes updated periodical and if there is a tradelimit which has the same price, the user becomes informed.
Because the market has already the currency stored, i dont want to store it again in the tradelimit model (not dry):
class Market << AR
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_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") }
...
end
#tradelimit.rb
class Tradelimit << AR
composed_of :price,
:class_name => "Money",
:mapping => [%w(price_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") }
...
end
How can I inherit now the currency from the parent (market) model?
I tried to override the currency method in the tradelimit model, but this doenst work..
Any suggestions? Thanks!
EDIT:
Or is there a way (without the composed_of feature) to provide this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有没有尝试过:
Have you tried: