我重构我的代码并尝试遵循约定“瘦控制器,胖模型”现在我尝试将查询移动到模型:
scope :scope1_department, where(sender_username: params[:username], recipient_username: params[:username])
scope :scope2_department, where(recipient_username: params[:username])
scope_department = scope1_department.merge(scope2_department).sort_by(&:created_at)
在我的控制器中,我有:
@messages = Message.scope_department(params[:username])
现在我的模型代码出现此错误:
undefined local variable or method `params' for #<Class:0x85d0b80>
我还尝试在我的模型中将 params[:username] 替换为简单的用户名,但随后出现错误:
undefined local variable or method `username' for #<Class:0x459fa50>
有一个类似的问题,但是你怎么能发现我做错了什么: Rails3 How can I use :params in named scope?
谢谢!