1

I'm using Devise 1.4.8

I want to assign the current logged-in user's id in callbacks method.

How to use current user's id in the model without passing it from the controller?

Is there any other way to do this in the model?

nishanthan
  • 460
  • 1
  • 5
  • 19
  • 1
    There is no way to access current_user in model. This is only available at controller level. What exactly you are trying to do with current_user in model? – maximus ツ Dec 12 '12 at 14:01
  • Duplicate of http://stackoverflow.com/questions/4149326/rails-devise-get-object-of-the-currently-logged-in-user – Noz Dec 12 '12 at 19:59
  • @Sush I want to assign the user_id column in the model(Product,..) before validation callbacks – nishanthan Dec 13 '12 at 06:27
  • then it is better to pass this from the controller. You can write some filter to assign that value to some global variable. – maximus ツ Dec 13 '12 at 07:30
  • @Sush, but i'm trying to validate, Is the assigned value is belongs to the currently logged-in user and how can i validate without getting the logged-in user object inside the model instead of passing it from the controller??? is this good practice?? – nishanthan Dec 13 '12 at 07:51
  • @CyleHunter thanks for your comment, i know its not possible to use Devise helper methods inside the model. I'm just looking to get the logged-in user object inside models by any new logic – nishanthan Dec 13 '12 at 07:57
  • just assign logged in user object(current_user) to some global variable let's say $var1, this $var1 can be accessible any where including model. – maximus ツ Dec 13 '12 at 09:03
  • Thanks @Sush, Using global variable in production consumes lot memory and led to bad performance – nishanthan Dec 13 '12 at 13:50

0 Answers0