作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Rails 3 中测试范围的最佳方法是什么。在 Rails 2 中,我会执行以下操作:
规范:
it 'should have a top_level scope' do
Category.top_level.proxy_options.should == {:conditions => {:parent_id => nil}}
end
最佳答案
撇开“如何测试”的问题不谈……这里是如何在 Rails3 中实现类似的东西……
在 Rails3 中,命名范围的不同之处在于它们只生成 Arel 关系运算符。
但是,调查!
如果您转到控制台并输入:
# All the guts of arel!
Category.top_level.arel.inspect
# Basic stuff:
=> [:table, :primary_key, :to_sql]
# and these to check-out all parts of your relation:
=> [:includes_values, :eager_load_values, :preload_values,
:select_values, :group_values, :order_values, :reorder_flag,
:joins_values, :where_values, :having_values, :limit_value,
:offset_value, :readonly_value, :create_with_value, :from_value]
# With 'where_values' you can see the whole tree of conditions:
Category.top_level.where_values.first.methods - Object.new.methods
=> [:operator, :operand1, :operand2, :left, :left=,
:right, :right=, :not, :or, :and, :to_sql, :each]
# You can see each condition to_sql
Category.top_level.where_values.map(&:to_sql)
=> ["`categories`.`parent_id` IS NULL"]
# More to the point, use #where_values_hash to see rails2-like :conditions hash:
Category.top_level.where_values_hash
=> {"parent_id"=>nil}
关于ruby-on-rails - 如何在 Rails 3 中测试范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3025103/
我是一名优秀的程序员,十分优秀!