Resyful authenticatio current_user in model

Hi, I am using Restful authentication for user authentication. I want the current logged in user object in my model property.rb Problem is current_user is not working on model.

How can I fix this I need that?

Please help me out.

Thanks, Mike

You have two options:

  1. Pass the current user from your controller into your model method: some_model.some_method(current_user)

  2. Use threads to set a class variable on the User model. You can either use the userstamp gem/plugin to provide you the functionality (User.stamper) or extract the necessary code and roll your own: http://github.com/grosser/userstamp

The first method respects the separation of MVC better, the second can be more convenient in some cases. It’s up to you to decide the best route.

Best regards

Peter De Berdt

Mike Disuza wrote:

Hi, I am using Restful authentication for user authentication.

[...]

This isn't directly related to your issue...but if you don't mind a bit of advice, I highly recommend removing restful_authentication from your project as soon as possible. It relies on unmaintainable generated code, and so is needlessly hard to work with. I recommend Authlogic instead.

Thanks, Mike

Best,

The third option is to make virtual attribute on the model and always set it when needed in the controller

class Model   attr_accessor :current_user

  def some_method     if current_user.admin?       #foo     else       #bar     end   end end

class FoosController

  def index     m = Model.find(...)     m.current_user = current_user     m.some_method()   end

end

Marnen: What's so 'unmaintainable' in your opinion?

Robert Pankowecki