以下测试验证 @rubish 的最新编辑是否有效。
Trip.all.or(:images.size => 0).or(:images => nil)
测试/单元/trip_test.rb
require 'test_helper'
class TripTest < ActiveSupport::TestCase
def setup
Trip.delete_all
end
test "criteria or" do
Trip.create(:images => nil)
Trip.create(:images => [])
Trip.create(:images => ['xyzzy'])
assert_equal(3, Trip.count)
puts "Trip.all images:#{Trip.all.to_a.map(&:images).inspect}"
trips_with_images_empty_or_nil = Trip.all.or(:images.size => 0).or(:images => nil).to_a
puts "trips_with_images_empty_or_nil images: #{trips_with_images_empty_or_nil.map(&:images).inspect}"
assert_equal(2, trips_with_images_empty_or_nil.size)
end
end
测试输出
Run options: --name=test_criteria_or
# Running tests:
Trip.all images:[nil, [], ["xyzzy"]]
trips_with_images_empty_or_nil images: [nil, []]
.
Finished tests in 0.009099s, 109.9022 tests/s, 219.8044 assertions/s.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips