在范围内传递参数 [英] Pass arguments in scope

查看:40
本文介绍了在范围内传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能提供一个如何使用的例子

Can someone provide an example on how to use

scope

和参数?

例如:

class Permission < ActiveRecord::Base
  scope :default_permissions, :conditions => { :is_default => true }
end

我有这个返回 default_permissions 的代码,我想将其转换为返回给定用户 (user_id) 的默认权限

I have this code that returns the default_permissions and I want to convert it to return the default permissions for a given user (user_id)

谢谢

推荐答案

新语法(ruby 1.9+),即使你不提供用户也能防止错误 -

new syntax (ruby 1.9+), that will prevent errors even if you don't supply the user -

scope :default_permissions_for, ->(user = nil) { ... }

这篇关于在范围内传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆