For your case, it sounds like it is faster to do this using
hash.index_by. I am just saying for larger hash size, it could be better
to have the database doing the work. I could be wrong on this.
Is memory access in hash.index_by is faster than SQL query ? I might
need to run some benchmark to see at what hash size, the database query
started to yield a better performance.
Performance aside, I prefer to use with_scope for easier to maintain
code. It is just my preference.
Leon Yeh
New Avenue Systems Inc.
Office: (626)254-1757 x880
[EMAIL PROTECTED]
Imagination Delivered. www.newavenue.net
Javan Makhmali wrote:
The page I'm working on has 10 different blurbs of text that I need to
retrieve. Are you saying it's faster to make 10 queries than to make 1
query and convert it to a hash using index_by?
On Apr 11, 2007, at 10:07 PM, Leon Yeh wrote:
Hi Javan, this probably not going to do what you want but I found
using with_scope create a more readable code, not to mention I think
it would be resulted a better performance than going thru hash one by
one, especially when the resulting hash is a large one. Database is
designed to handle search better than going thru the hash in linear
fashion.
http://media.railscasts.com/videos/005_using_with_scope.mov
Watch this screencast, and let me know what do you think?
Leon Yeh
New Avenue Systems Inc.
Office: (626)254-1757 x880
[EMAIL PROTECTED]
Imagination Delivered. www.newavenue.net
Javan Makhmali wrote:
Even better!
On Apr 11, 2007, at 9:51 PM, John Parker wrote:
On 4/11/07, Javan Makhmali <[EMAIL PROTECTED]> wrote:
I'd love to do
@blurbs = Blurb.find(:all)
@about_blurb = @blurbs.give_me_the_one_where_name_is('about')
ActiveSupport adds #index_by to Enumerable which seems like it might
be useful here:
<URL:http://api.rubyonrails.org/classes/Enumerable.html#M001100>
@blurbs = Blurb.find(:all).index_by(&:name)
@blurbs would be a Hash keyed on the blurb names, allowing you to grab
@blurbs['about'], @blurbs['contact'], etc.
-John
--John Parker
[EMAIL PROTECTED]
_______________________________________________
Sdruby mailing list
[EMAIL PROTECTED]
http://lists.sdruby.com/mailman/listinfo/sdruby
------------------------------------------------------------------------
_______________________________________________
Sdruby mailing list
[EMAIL PROTECTED]
http://lists.sdruby.com/mailman/listinfo/sdruby
_______________________________________________
Sdruby mailing list
[EMAIL PROTECTED]
http://lists.sdruby.com/mailman/listinfo/sdruby
------------------------------------------------------------------------
_______________________________________________
Sdruby mailing list
[EMAIL PROTECTED]
http://lists.sdruby.com/mailman/listinfo/sdruby
_______________________________________________
Sdruby mailing list
[EMAIL PROTECTED]
http://lists.sdruby.com/mailman/listinfo/sdruby