Topic: find method

Hey question about find. Can I use this method to search arrays with?

I've found my self making abunch of calls to the same table, when I could really just use one call then filter though it. I was trying to use find but just kept getting an error telling me 2 arguments for 1. I know that I could proberly filter thought the hash, but I was just wondering if I could use find.

thanks

--

Re: find method

Are you referring to the Enumerable "find" method that is in Ruby core? Rails overrides this method for doing finds with ActiveRecord. If you want to access this find method, use "detect" instead - which does the same thing.

But, if what you really want is to use the ActiveRecord "find" method on an Array - then AFAIK this is not possible. You'll need to enumerate the array and do your own searching. Check out the "select" method for Array/Enumerable for doing this.

Railscasts - Free Ruby on Rails Screencasts

Re: find method

thanks for that. I'm working with a bunch of data and it seems more economical just to call the data once and sift through it smile instead of making 10 calls to the db

--

Re: find method

It all comes down to how many rows are in the db vs how many times you'll call it.  If you're going to retrieve 10,000 rows and loop through them you'd be better off with separate queries.  If you're only grabbing a few then you should manage them inside your app.