I want to show prices for products, and read that using Decimal is far more reliable than Integer or using Floats.
I would like to know how to extract the actual number 17.99 from the database when it is stored as <BigDecimal:646c8b8,'0.2E2',9(18)
for example. I need to show this value in a form.
So far I have:
f.input :image_prices, as: :check_boxes
Which will output in my view:
#<IMAGEPRICE:0X007F0431BB0850>
#<IMAGEPRICE:0X007F0431BB0710>
#<IMAGEPRICE:0X007F0431BB05D0>
#<IMAGEPRICE:0X007F0431BB0490>
But when saving the correct ids are stored. The model relationships are as follows:
class Image < ActiveRecord::Base
has_many :image_options, dependent: :destroy
has_many :image_prices, through: :image_options
end
class ImageOption < ActiveRecord::Base
belongs_to :image_price
belongs_to :image
end
class ImagePrice < ActiveRecord::Base
has_many :image_options
has_many :images, through: :image_options
end
I am so stuck at the moment and have tried for a good 3 hours now to no avail.
If the only problem is the image prices showing in the check box list as #<IMAGEPRICE:0X007F0431BB0850>
, just define a to_s
method in your ImagePrice
model and have it return a string representation of the price. For example, if the decimal value of that price is stored in a price
attribute, your method would look like
class ImagePrice < ActiveRecord::Base
# ...
def to_s
price.to_s
end
end