我想current_user在我的模型中使用 Devise 的助手,这样我就可以Shop在Dress模型中使用before_save.
这不起作用:
# Dress model
before_save :set_currency
def set_currency
self.currency = current_user.shop.currency
end
它确实在控制器中工作:
def create
@dress = current_user.shop.dresses.create(params[:dress])
@dress.update_column(:currency, current_user.shop.currency)
end
但它似乎效率低下,因为它会做一个UPDATEafter COMMIT。StackOverflow 上的其他用户说不current_user应该在模型中使用。有没有其他方法可以current_user在模型中访问?