views:

301

answers:

1

Hello, in my project.rb model, I'm trying to create a scope with a dynamic variable:

scope :instanceprojects, lambda { 
    where("projects.instance_id = ?", current_user.instance_id)
} 

I get the following error: "undefined local variable or method `current_user' for #"

Where in the controller I can access current_user.instance_id... Is there a reason the model can't access it and a way to get access? Also, is this the right place to create a scope like the above, or does that belong in the controller?

Thanks

+4  A: 

This doesn't make much sense, as you already pointed. The current_user doesn't belong to model logic at all, it should be handled on the controller level.

But you can still create scope like that, just pass the parameter to it from the controller:

scope :instanceprojects, lambda { |user|
    where("projects.instance_id = ?", user.instance_id)
} 

Now you can call it in the controller:

Model.instanceprojects(current_user)
mdrozdziel
that's awesome. trying it now
AnApprentice
Worked great. Thank you!
AnApprentice