【发布时间】:2013-08-30 11:35:51
【问题描述】:
在我的 rails 应用程序中,我有一些用户可以付费的活动。我需要能够根据当前用户更改活动价格。
*我知道已经有很多关于在模型中访问 current_user 的主题,但这不是我想要的。*
我有以下 2 个模型(非常简化)。 Checkout 正在管理与事件相关的所有支付事项(我需要它在一个单独的模型中,因为在真实的应用程序中它与事件具有多态关联)。
class Event < ActiveRecord::Base
attr_accessible :ticket_price, :checkout
has_one :checkout
checkout_price
# Here I'd like to be able to use the current_user to change price accordingly
# Example: user.premium? ? ticket_price/2 : ticket_price
ticket_price
end
end
class Checkout < ActiveRecord::Base
attr_accessible :event
belongs_to :event
def total
event.checkout_price
end
def free?
total == 0
end
end
我显然可以定义checkout_price(user),但我必须在任何地方传递它(例如event.checkout_price(current_user)、checkout.total(current_user)、checkout.free?(current_user))。
我知道从模型中访问current_user 是一种不好的做法(我绝对不想这样做),但是在我的情况下,除了一直将current_user 作为参数传递之外,还有其他解决方案吗?
【问题讨论】:
标签: ruby-on-rails ruby