Hi, I want to display count of all records with particular field value from database onto my view. How should I do that? PLs tell me. Thanx. Prash -- Posted via http://www.ruby-forum.com/.
> I want to display count of all records with particular field value from > database onto my view. How should I do that?I think you can do it as follows: in yr controller: @count = [model_to_be_queried].find(:all, ["field_name = ?", value]).size and then use <%=h @count %> in your view So for all records in your chicken table with 10 eggs @count = Chicken.find(:all, ["eggs = ?", 10]).size -- Posted via http://www.ruby-forum.com/.
Jonathan Viney
2006-May-08 12:40 UTC
[Rails] Re: How to get count of records from database?
If you only want the number of records it will be a lot faster to use count rather than find as AR won''t have to instantiate all the objects. Person.count :conditions => [''age = ?'', 29] http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000823 -Jonny. On 5/8/06, Piers <piers.young@gmail.com> wrote:> > I want to display count of all records with particular field value from > > database onto my view. How should I do that? > > I think you can do it as follows: > > in yr controller: > > @count = [model_to_be_queried].find(:all, ["field_name = ?", > value]).size > > and then use <%=h @count %> in your view > > So for all records in your chicken table with 10 eggs > > @count = Chicken.find(:all, ["eggs = ?", 10]).size > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jonathan Conway
2006-May-08 12:45 UTC
[Rails] Re: How to get count of records from database?
Controller ========@cheese_number = Cheese.count(:conditions => "colour = ''red''") View ===<%= @cheese_numer %> Cheers Jonathan ------ http://www.agileevolved.com Prashant Tiwari wrote:> Hi, > > I want to display count of all records with particular field value from > database onto my view. How should I do that? > PLs tell me. > Thanx. > Prash >
Reasonably Related Threads
- Can anybody tell me step by step how validate data on form?
- pls help me regarding Maths round up function.....
- How to disable textbox in Ruby?
- undefined method `validates_presence_of'' for #<ProductsContr
- Problem regarding accessing attributes from object...