Best way to filter a collection?

What is the best way to filter a collection of objects pulled from a database before display? I can think of 3 and have run into problems with each:

1) Using SQL :joins and :conditions on Model.find. Problems: SQL is ugly and the attributes coming back on my objects are being overwritten (the ids that I need to pass to edit and delete actions are coming back as the ids of the last table joined, not the ids of the objects I'm requesting).

2) Use Model.find(:all) then iterate through the collection (for @object in @objects) to remove the ones that don't meet my criteria. Problem: I use @objects.delete(@object) to remove the unwanted objects from the collection, but it seems that (for @object in @objects) has some kind of internal counter and deleting an object from the collection shortens the collection by 1 so that the next iteration through @objects ends up skipping an object--in other words, if I were trying to delete all the objects, only every OTHER object would be deleted.

3) Use Model.find(:all) then filter each object in the view. Problem: This logic shouldn't be in the view, though at this point it's the only way I know I can get it to work.

Any help is appreciated, Isaac

isaac wrote:

What is the best way to filter a collection of objects pulled from a database before display? I can think of 3 and have run into problems with each:

1) Using SQL :joins and :conditions on Model.find. Problems: SQL is ugly and the attributes coming back on my objects are being overwritten (the ids that I need to pass to edit and delete actions are coming back as the ids of the last table joined, not the ids of the objects I'm requesting).

2) Use Model.find(:all) then iterate through the collection (for @object in @objects) to remove the ones that don't meet my criteria. Problem: I use @objects.delete(@object) to remove the unwanted objects from the collection, but it seems that (for @object in @objects) has some kind of internal counter and deleting an object from the collection shortens the collection by 1 so that the next iteration through @objects ends up skipping an object--in other words, if I were trying to delete all the objects, only every OTHER object would be deleted.

3) Use Model.find(:all) then filter each object in the view. Problem: This logic shouldn't be in the view, though at this point it's the only way I know I can get it to work.

Any help is appreciated, Isaac

The easiest way sort of depends on what you are doing, so an example would be helpful. In general #1 is your best approach. Just don't retrieve records you don't need.

#2 won't scale. If you have more than a few objects in your table, you end up creating them all, which chews up memory and slows down the server.

#3 has the same problem in addition to doing business logic in a view...

In general, something like..

Model.find(:all, :conditions=>["user = ?", user_id]) or model.users (if you have your associations set up right)

will be your best bets.

_Kevin