Adding a scope to rails 3 app upsets.

I use Devise for authentication. Its variable "current_user" is widely used in the application without any problems. Then I added the following area to the model named "Response"

scope :user_answers, where (:user_id => current_user.id)

The result of this area is part of another area in the model named Question

scope :qs_w_user_ans, joins(:questions) & Answer.user_answers

Since then I get the following error:

undefined local variable or method `current_user'

Why is this going to happen?

Thank.

+3
source share
1 answer

As Zabba said, you cannot access current_userin your model. You can pass an argument to your scope:

scope :user_answers, lambda {|user_id| where(:user_id => user_id)}

Then you can call a scope like this (assuming you are calling it from a controller or view):

Answer.user_answers(current_user.id)

Update

, , - :

scope :qs_w_user_ans, lambda {|user_id| joins(:questions) & Answer.user_answers(user_id)}

current_user.id, qs_w_user_ans. user_id user_answers, . ( ).

+6

All Articles