Hello RoR people, First time here. I have a question about calculations in rails. For example i have two tables: categories id name and questions: id category_id name How I can count questions in each category? Please don''t give me recepies like: :include => [:questions] or something like: @categories.each { |c| c.questions.count } Because those methods is not so much optimized, to run them on a production server. Thank you, Dmitry for Estonia. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Solid wrote:> Please don''t give me recepies like:My company uses "recepies" like that on production servers. Could you be more specific? Which methods are you against using and why? Would you prefer writing your own SQL queries? Have you looked at the find_by_sql and count_by_sql methods? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Solid wrote:> Hello RoR people, > > First time here. > > I have a question about calculations in rails. > > For example i have two tables: > > categories > id > name > > and questions: > id > category_id > name > > How I can count questions in each category? > > Please don''t give me recepies like: :include => [:questions] or > something like: @categories.each { |c| c.questions.count } Because > those methods is not so much optimized, to run them on a production > server.@category.questions.size From the API docs: Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn''t been loaded and calling collection.size if it has. If it''s more likely than not that the collection does have a size larger than zero and you need to fetch that collection afterwards, it''ll take one less SELECT query if you use length. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---