让你 减少 ActiveRecord 查询 所要敲的代码。
通常我们会这么写:
Article.where ['created_at >= ?', 2.weeks.ago]
而不会这么写,因为这被认为是 ‘不安全的’
Article.where{created_at >= 2.weeks.ago}
而现在 squeel 就是让你这么 ‘简洁’, 安全等其它问题由它来解决。你只要关注于 ‘真正要做的事’ 即可。
使用前提一
Squeel.configure do |config|
# To load hash extensions (to allow for AND (&), OR (|), and NOT (-) against
# hashes of conditions)
config.load_core_extensions :hash # 就这一行 !
# To load symbol extensions (for a subset of the old MetaWhere functionality,
# via ARel predicate methods on Symbols: :name.matches, etc)
# config.load_core_extensions :symbol
# To load both hash and symbol extensions
# config.load_core_extensions :hash, :symbol
end
为什么?
因为它能给我们的 ‘查询’ 方法传递一个 block, 也就是 ‘花括号’ {} ,并且在 花括号里书写 DSL, 查询方法是能够 ‘消化’的。
10 个特点
Stubs
KeyPaths
Predicates
Predicate aliases
Compound conditions
Sifters
Subqueries
Joins
Functions
SQL Operators