Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
foo_count = Foo.where(some_id: @some_id, :bar_val => 100).count
这是计算符合特定标准的对象的最佳方法还是可以优化?
如果@some_id指的是 ActiveRecord 关联(即SomeObjecthas_many Foo),您可以执行以下操作:
@some_id
SomeObject
Foo
foo_count = @some_object.foos.where(:bar_val => 100).count