Hi. I have few models - user, task, file class User < ActiveRecord::Base has_many :tasks end class Task < ActiveRecord::Base belongs_to :user has_and_belongs_to_many :files end class File < ActiveRecord::Base has_and_belongs_to_many :tasks end I want to know files count for some user. is there some ''right'' way to do it without getting all tasks and looping it? P.S. there is really many tasks and files. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If you are using rails 3, maybe this will help you: http://railscasts.com/episodes/202-active-record-queries-in-rails-3 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
sl_bug wrote in post #977638:> I want to know files count for some user. is there some ''right'' way to > do it without getting all tasks and looping it? > > P.S. there is really many tasks and files.ActiveRecord includes aggregate calculations for this purpose that perform the counts, sums, averages, etc. at the SQL layer: http://railsapi.com/doc/rails-v3.0.3/classes/ActiveRecord/Calculations.html -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yea, but how to use it with such associations? On Wed, 2011-01-26 at 20:14 +0100, Robert Walker wrote:> sl_bug wrote in post #977638: > > I want to know files count for some user. is there some ''right'' way to > > do it without getting all tasks and looping it? > > > > P.S. there is really many tasks and files. > > ActiveRecord includes aggregate calculations for this purpose that > perform the counts, sums, averages, etc. at the SQL layer: > > http://railsapi.com/doc/rails-v3.0.3/classes/ActiveRecord/Calculations.html > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
seems like i found how to do it for one user File.where(:users => {:id => 1}).count(:all, :include => {:tasks => :user}) but how to get hash with ''user'' -> ''files count'' using one query, or may be even use something like counter cache. On Wed, 2011-01-26 at 20:14 +0100, Robert Walker wrote:> sl_bug wrote in post #977638: > > I want to know files count for some user. is there some ''right'' way to > > do it without getting all tasks and looping it? > > > > P.S. there is really many tasks and files. > > ActiveRecord includes aggregate calculations for this purpose that > perform the counts, sums, averages, etc. at the SQL layer: > > http://railsapi.com/doc/rails-v3.0.3/classes/ActiveRecord/Calculations.html > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.