I''m using ActiveRecord to spit out a list with find_all. In my template, I''m using the options_form_collection_for_select with that collection and when the template loads, it''s performing a select for every object in the collection. Needless to say, that''s not the most desirable behaviour. So I decided I''d give caching the results a go (it''s static data), so I created a class variable on the ActiveRecord and wrote a method to lazily load the list. It still does all the selects, and I''m guessing it''s because ActiveRecord grabs the data each time to make sure it''s not stale. Any ideas how to a) get options_from_collection_for_select to grab all the rows at once or b) cache ActiveRecord results? I''m absolutely floored by how fast I''m developing with Rails. Stuff that would have taken me over a week in Java + WebWork2 + Velocity + Hibernate has taken me a little over a day with Rails. I''m not even going to try to compare it to my current client''s project which requires Struts Anoop
David Heinemeier Hansson
2004-Oct-23 11:17 UTC
Re: ActiveRecord and options_from_collection_for_select
> Any ideas how to a) get options_from_collection_for_select to grab all > the rows at once or b) cache ActiveRecord results?Depending on what data you need, it''s very likely that you can use the piggy-back technique: http://activerecord.rubyonrails.org/show/Performance Also, do make sure that it''s actually a problem that you''re fetching many rows. I''ve seen people go into optimization mode on rarely used pages that perhaps executes 5-10 queries "too many". That''s a waste of time! You should optimize because you need to, not because you don''t like the look of a few extra queries in the log. Optimization is excess baggage, only bring it along if you''re positive you''re going to need it.> I''m absolutely floored by how fast I''m developing with Rails. Stuff > that would have taken me over a week in Java + WebWork2 + Velocity + > Hibernate has taken me a little over a day with Rails. I''m not even > going to try to compare it to my current client''s project which > requires StrutsThat''s an awesome quote, Anoop. Thank you for saying that! -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain
Anoop Ranganath
2004-Oct-23 13:07 UTC
Re: ActiveRecord and options_from_collection_for_select
>> Any ideas how to a) get options_from_collection_for_select to grab >> all the rows at once or b) cache ActiveRecord results? > > Depending on what data you need, it''s very likely that you can use the > piggy-back technique: > http://activerecord.rubyonrails.org/show/Performance > > Also, do make sure that it''s actually a problem that you''re fetching > many rows. I''ve seen people go into optimization mode on rarely used > pages that perhaps executes 5-10 queries "too many".No doubt! Make it work, make it work right, make it work fast. In this case, though, it''s making over 100 database queries to fetch this select list, causing a very noticeable decrease in load time. The piggy-back method isn''t really applicable since they''re all in one table. I''ll try reducing it down to one find_by_sql query. Honestly, the use of a select in this case is a design flaw, but I can''t think of an alternative yet. Another unrelated question. Is there anything that corresponds to the application scope in Servlets? Where can I toss things that I want everyone to have access to? It''s stuff that really doesn''t belong in the database. Also, do you know about how many users the largest rails app in production supports? Anoop
David Heinemeier Hansson
2004-Oct-23 13:19 UTC
Re: ActiveRecord and options_from_collection_for_select
> The piggy-back method isn''t really applicable since they''re all in one > table. I''ll try reducing it down to one find_by_sql query. Honestly, > the use of a select in this case is a design flaw, but I can''t think > of an alternative yet.Interesting. Yearh, it sounds like find_by_sql is the way to go then. Lots of intricate relationships are best queried by straight sql.> Another unrelated question. Is there anything that corresponds to the > application scope in Servlets? Where can I toss things that I want > everyone to have access to? It''s stuff that really doesn''t belong in > the database.No. Rails follows the lead of PHP in its "share nothing" policy. This makes Rails much easier to scale as you can push everything down to the database, or another service, that can then handle the concurrency issues. If you really need it, I''d look into DRb. With DRb you can setup a separate service that can be queried from your Rails app using a RMI-like interface. Be sure to make that DRb service thread-safe, though!> Also, do you know about how many users the largest rails app in > production supports?Basecamp is the poster child for Rails. It services "tens of thousands of users in 40+ countries". We haven''t released any data on concurrency, though. So that''s more of a marketing statement than a technical one. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain