association collection find vs ordinary array find

1) Maybe the trip to the db is more expensive than filtering in memory.
Is it assumed that this will never be the case?

Most likely, this *is* the case. The database server is specialized to be able to do these kinds of operations, so the database server is probably going to be able to do the restriction faster (and in compiled code, no less), than Ruby. Also, you have the overhead of sending all of the data -- including that which you're just going to throw away--over the wire to the middleware. Taking just those two into account, I think it's a safe bet that using the database server to handle the filtering is going to be a win.

But there's no pretty way to do that filtering in memory.

I haven't looked at the ActiveRecord source, but I'd think it's likely that Array.find has been aliased rather than just left hanging. If you're motivated to do so, you might be able to work something to your liking using the alias.

Michael Glaesemann
grzm seespotcode net

> 1) Maybe the trip to the db is more expensive than filtering in
> memory.
> Is it assumed that this will never be the case?

Most likely, this *is* the case. The database server is specialized
to be able to do these kinds of operations, so the database server is
probably going to be able to do the restriction faster (and in
compiled code, no less), than Ruby.

For simple fields, yes.
But a lot of times the filter is based on object methods, algorithms,
Ruby code, and you need to do it in Ruby. (Otherwise, we'd ditch
Rails, and just put the entire app/models in db stored procedures)

> But there's no pretty way to do that filtering in memory.

Array#detect

Yes, I do think that AR snatching the #find method can be very
confusing (caused some wierdness till I figured out what happened)