Resyful authenticatio current_user in model

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.


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:

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:

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



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?

class FoosController

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


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

Robert Pankowecki