【发布时间】:2018-07-01 03:09:25
【问题描述】:
在我的应用程序控制器中,我根据地理位置设置了一个货币变量:
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
我有带有定价方法和多种货币、多种价格的模型产品,即:一种产品可以有多种货币和自定义定价。
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
在 Model Product.price 中访问 @currency 的最佳方式是什么?或者我怎样才能告诉方法只在@currency 中返回价格?这可能不是最好的处理方式,所以请指教。
【问题讨论】:
-
我相信this 可能会对你有所帮助
标签: ruby-on-rails