-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the best way to assign a resource to an attribute? #12
Comments
I found that I was able to do this with write_attribute(). But I would still like to know the canonical or expected usage. |
I'm still using this gem and I suppose you could call it maintained, it's just been stable enough that no changes were required. The following code was implemented for support of latinum/lib/latinum/resource.rb Lines 40 to 57 in acf12ea
A bank object is only needed for formatting and converting resources, it should not be needed for basic interactions with "I have X units of Y." For actual values, you need to write:
If you want rich input, you do need to use the bank configured with the currencies you want. Otherwise, how can you tell In order to use the bank object, you should configure it in the controller to map the user input before creating the model with attribute values. e.g.
I can write updated documentation with working examples if that helps. |
I wonder if we should make |
Okay, so with a few minor changes, we can make this work a bit more nicely:
The code is: require 'latinum/currencies/global'
class T < ApplicationRecord
BANK = Latinum::Bank.new.tap do |bank|
bank.import(Latinum::Currencies::Global)
end
serialize :amount, coder: BANK
end This uses the bank as a coder, which accepts a wider range of inputs. However, if there is ambiguity, it is resolved according to the bank's internal priority, e.g. |
I've released v1.8.0 with the required changes and added documentation: https://ioquatix.github.io/latinum/guides/activerecord-integration/index.html |
I am exploring whether to use this gem for a multi currency accounting system. It is old and presumably no longer developed but I liked the concept of being able to store the value and currency together and ensure that I would not be summing unlike quantities.
I tried using the ActiveRecord integration and setting the amount column to type string on my
line_items
model.Then I tried creating with something like:
entry.line_items.create!(amount: '$40145.00')
In the LineItem class, I have:
serialize :amount, Latinum::Resource
This did not work. Amount is nil. I tried overriding
amount=(value)
and parsing:At this point the attribute is still nil. I tried setting it a variety of ways. None seemed to work.
What is the recommended way to do this?
The text was updated successfully, but these errors were encountered: