In my Application Controller I set up a currency variable based on geolocation:
class ApplicationController < ActionController::Base
before_action :currency
protected
def currency
cookies[:country] ||= request.location.country
case cookies[:country]
when "MY"
c = "MYR"
when "SG"
c = "SGD"
else
c = "USD"
end
@currency = Currency.find_by(name: c)
end
end
I have Model Product with price method and many currencies, many prices, ie: one product can have multiple currencies with custom pricing.
class Product < ApplicationRecord
has_many :prices
has_many :currencies, through: :prices
def price
# how to access @currency?
end
end
class Price < ApplicationRecord
belongs_to :product
belongs_to :currency
end
class Currency < ApplicationRecord
has_many :prices
has_many :products, through: :prices
end
What is the best way to access @currency in the Model Product.price? Or how can I tell the method to return the price only in the @currency? This is probably not the best way to deal it so please advice.