I have a query that selects all records from a db. In the view i loop through the records. Does rails setup a variable that holds the amount of records the query returned? Similar to Coldfusions recordcount? -- 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 -~----------~----~----~----~------~----~------~--~---
hi, from doc Examples for counting all: Person.count # returns the total count of all people Examples for count by conditions and joins (this has been deprecated): Person.count("age > 26") # returns the number of people older than 26 Person.find("age > 26 AND job.salary > 60000", "LEFT JOIN jobs on jobs.person_id = person.id") # returns the total number of rows matching the conditions and joins fetched by SELECT COUNT(*). Examples for count with options: Person.count(:conditions => "age > 26") Person.count(:conditions => "age > 26 AND job.salary > 60000", :include => :job) # because of the named association, it finds the DISTINCT count using LEFT OUTER JOIN. Person.count(:conditions => "age > 26 AND job.salary > 60000", :joins => "LEFT JOIN jobs on jobs.person_id = person.id") # finds the number of rows matching the conditions and joins. Person.count(''id'', :conditions => "age > 26") # Performs a COUNT(id) Person.count(:all, :conditions => "age > 26") # Performs a COUNT(*) (:all is an alias for ''*'') Note: Person.count(:all) will not work because it will use :all as the condition. Use Person.count instead. might help regards gaurav On 12/22/06, frank <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I have a query that selects all records from a db. In the view i loop > through the records. > > Does rails setup a variable that holds the amount of records the query > returned? Similar to Coldfusions recordcount? > > -- > 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 -~----------~----~----~----~------~----~------~--~---
On Dec 21, 11:16 pm, "gaurav bagga" <gaurav.v.ba...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Examples for counting all: > > Person.count # returns the total count of all peopleIf you only want the counts then the above method is considerably faster. If you want the records AND the count then just use "length". ActiveRecord finds return an array, and array.length will tell you how many records were returned. people = Person.find(:all) puts "#{people.length} records returned" people.each {|person| puts person.name} Aaron --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Aaron wrote:> On Dec 21, 11:16 pm, "gaurav bagga" <gaurav.v.ba...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Examples for counting all: >> >> Person.count # returns the total count of all people > > If you only want the counts then the above method is considerably > faster. If you want the records AND the count then just use "length". > ActiveRecord finds return an array, and array.length will tell you how > many records were returned. > > people = Person.find(:all) > puts "#{people.length} records returned" > people.each {|person| puts person.name} > > AaronLength is what I was looking for, thank you. -- 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 -~----------~----~----~----~------~----~------~--~---