Ruby-on-rails 向作用域添加参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15229000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding parameter to a scope
提问by Bohn
I have a ActiveRecord query for example like this:
例如,我有一个 ActiveRecord 查询,如下所示:
@result = stuff.limit(10)
where stuff is a active record query with where clauses, order by, etc...
where stuff 是一个带有 where 子句、order by 等的活动记录查询...
Now I thought why to pass magic numbers like that to the controller? So do you think is it a good practice to define a scope for "limit(10)" and use that instead? and how would the syntax look like?
现在我想为什么要将这样的幻数传递给控制器?那么您认为为“limit(10)”定义一个范围并使用它是一个好习惯吗?以及语法是什么样的?
采纳答案by Dave Newton
The scope would look like any other (although you may prefer a class method), e.g.,
范围看起来像任何其他范围(尽管您可能更喜欢类方法),例如,
class Stuff < ActiveRecord::Base
def self.lim
limit(3)
end
end
> Stuff.lim.all
=> [#<Stuff id: 1, name: "foo", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
#<Stuff id: 2, name: "bnar", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
#<Stuff id: 3, name: "baz", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">]
> Stuff.all.length
=> 8
If you always(or "almost" always) want that limit, use a default scope:
如果您总是(或“几乎”总是)想要该限制,请使用默认范围:
class Stuff < ActiveRecord::Base
attr_accessible :name, :hdfs_file
default_scope limit(3)
end
> Stuff.all
=> [#<Stuff id: 1, name: "foo", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
#<Stuff id: 2, name: "bnar", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">,
#<Stuff id: 3, name: "baz", created_at: "2013-03-01 17:58:32", updated_at: "2013-03-01 17:58:32">]
> Stuff.all.length
=> 3
To skip the default scope:
要跳过默认范围:
> Stuff.unscoped.all.size
=> 8
回答by Andre Bernardes
There are indeed multiple ways of doing such, class methods are one as pointed out by @Dave Newton. If you'd like to use scopes, here's how:
确实有多种方法可以做到这一点,@Dave Newton 指出的类方法是其中一种。如果您想使用范围,方法如下:
scope :max_records, lambda { |record_limit|
limit(record_limit)
}
Or with the Ruby 1.9 "stabby" lambda syntax and multiple arguments:
或者使用 Ruby 1.9 “stabby” lambda 语法和多个参数:
scope :max_records, ->(record_limit, foo_name) { # No space between "->" and "("
where(:foo => foo_name).limit(record_limit)
}
If you'd like to know the deeper differences between scopes and class methods, check out this blog post.
如果您想了解作用域和类方法之间更深层次的区别,请查看这篇博文。
Hope it helps. Cheers!
希望能帮助到你。干杯!
回答by AnkitG
Well Scopes are meant for this
Well Scopes 就是为了这个
Scoping allows you to specify commonly-used Arel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as where, joins and includes. All scope methods will return an ActiveRecord::Relation object which will allow for further methods (such as other scopes) to be called on it.
范围允许您指定常用的 Arel 查询,这些查询可以作为关联对象或模型上的方法调用进行引用。通过这些范围,您可以使用之前介绍的所有方法,例如 where、joins 和 includes。所有范围方法都将返回一个 ActiveRecord::Relation 对象,该对象将允许在其上调用更多方法(例如其他范围)。
Source: http://guides.rubyonrails.org/active_record_querying.html#scopes
来源:http: //guides.rubyonrails.org/active_record_querying.html#scopes
So if you feel that there are some common queries which you have, or you need some kind of chaining in your queries which are common to many. Then i will suggest you to go for scopes to prevent repetition.
因此,如果您觉得您有一些常见的查询,或者您的查询中需要某种对许多常见的查询链接。然后我会建议你去寻找范围以防止重复。
Now to answer how the scope will look like in your case
现在回答范围在您的情况下的样子
class YourModel < ActiveRecord::Base
scope :my_limit, ->(num) { limit(num)}
scope :your_where_condition, ->(num) { where("age > 10").mylimit(num) }
end
回答by deep gupta
Pass parameters in Rails scope
在 Rails 范围内传递参数
Definition of scope
范围定义
scope :name_of_scope, ->(parameter_name) {condition whatever you want to put in scope}
Calling Method
调用方式
name_of_scope(parameter_name)
回答by Manoj Datt
Scope in Rails model with parameter:
带参数的 Rails 模型中的范围:
scope :scope_name, -> (parameter, ...) { where(is_deleted: parameter, ...) }
Or:
或者:
scope :scope_name, lambda{|parameter, ...| where(is_deleted:parameter, ...)}

