I want to use Devise’s current_user helper in my model so I can save a Shop‘s currency in a Dress model using before_save.
This won’t work:
# Dress model
before_save :set_currency
def set_currency
self.currency = current_user.shop.currency
end
It does work in the controller:
def create
@dress = current_user.shop.dresses.create(params[:dress])
@dress.update_column(:currency, current_user.shop.currency)
end
but it seems inefficient since it’s gonna do an UPDATE after COMMIT. Other users on StackOverflow said that current_user should not be used in model. Is there any other way to access current_user in model?
Use .build instead of .create in controller to set value to currency attribute of Dress model.