hi guys, got this problem... in my controller, when i wanna do a @temp.count it fails... then i went to try counter = @temp.count which also failed.. thus, i feel that the count is not available for ActiveRecord::Base..so i went to the Model class eg. user.rb so in user.rb : ... def count_uesr count = 0 self.each do |record| count ++ end return count end then it ended up having undefined method for ''each'' .... omg ... anyone can suggest how to go about doing it??? thanks thanks -- Posted via http://www.ruby-forum.com/.
What gives you the idea that an object derives from ActiveRecord::Base is some sort of collection? It''s not, it''s just a single object, and doesn''t have methods like count or each. regards, Craig bao lee wrote:> hi guys, got this problem... > > in my controller, when i wanna do a @temp.count it fails... > > then i went to try counter = @temp.count which also failed.. > > thus, i feel that the count is not available for ActiveRecord::Base..so > i went to the Model class eg. user.rb > > so in user.rb : > > ... > def count_uesr > count = 0 > self.each do |record| > count ++ > end > return count > end > > then it ended up having undefined method for ''each'' .... > > omg ... anyone can suggest how to go about doing it??? >
Craig Ambrose wrote:> What gives you the idea that an object derives from ActiveRecord::Base > is some sort of collection? It''s not, it''s just a single object, and > doesn''t have methods like count or each. > > regards, > > Craigwell.... think i did not specify it properly, actually there is a find statement before i did a count... so does that answer your doubt???.... and anyway, the possibility of doing a Model.count gives me that idea..... any idea how to go about doing it??? -- Posted via http://www.ruby-forum.com/.
On Apr 27, 2006, at 12:36 AM, bao lee wrote:> Craig Ambrose wrote: >> What gives you the idea that an object derives from >> ActiveRecord::Base >> is some sort of collection? It''s not, it''s just a single object, and >> doesn''t have methods like count or each. > > well.... think i did not specify it properly, actually there is a find > statement before i did a count... so does that answer your > doubt???.... > and anyway, the possibility of doing a Model.count gives me that > idea.....Ah... In that case, you need to provide the full context of your problem. If you can show us the code that''s not working and which statement is throwing the error, we should be able to do a much better job of helping you out... -Brian
bao lee wrote:> Craig Ambrose wrote: >> What gives you the idea that an object derives from ActiveRecord::Base >> is some sort of collection? It''s not, it''s just a single object, and >> doesn''t have methods like count or each. >> >> regards, >> >> Craig > > well.... think i did not specify it properly, actually there is a find > statement before i did a count... so does that answer your doubt???.... > and anyway, the possibility of doing a Model.count gives me that > idea..... > > any idea how to go about doing it???To count all records Model.count Model.count :conditions => [''name = ?'', ''foo''] To count record in a collection @models = Model.find(:all) @models.size Does that answer your question? -- Posted via http://www.ruby-forum.com/.
Brian Hughes wrote:> On Apr 27, 2006, at 12:36 AM, bao lee wrote: >> idea..... > Ah... In that case, you need to provide the full context of your > problem. If you can show us the code that''s not working and which > statement is throwing the error, we should be able to do a much > better job of helping you out... > > -Briansorry about that, cos it''s a small part of a search function which is too big to display all... that''s why i summarized it... well, here''s the code... class AdminController < ApplicationController ... def search items_per_page = 10 case @request.method when :post @all_record = Record.new @all_record.search_param(@params[:query]) counter = @all_events.size @record_pages = Paginator.new self, counter,items_per_page end ... ok, so after i did a @all_record.search_param(@params[:query]), i also need to find out how many records there are in the @all_record for me to use the Paginator, but one thing that went wrong is that maybe i am not sure wat kind of variable has @all_record turned out to be. This is the latest change i have made which still doesnt work which i tot the @all_records would become an array of records which apparently is not. so do i have to write a count method in the Record class to count the number of records or there is already a method which i can call upon to do it?? thanks brian and craig for your assistance, hope you all can provide me with further solution. :) this is the model code.... class Record < ActiveRecord::Base has_one :payment validates_presence_of :session_id def search_param(query_value) sql = Array.new args = Array.new self.attributes.each do |key| sql.push "#{key} LIKE ?" args.push "%#{query_value}%" end Event.find_all([sql.join('' or ''),*args]) end end -- Posted via http://www.ruby-forum.com/.
> ok, so after i did a @all_record.search_param(@params[:query]), i also > need to find out how many records there are in the @all_record for me to > use the Paginator, but one thing that went wrong is that maybe i am not > sure wat kind of variable has @all_record turned out to be.Model.find(1) #=> returns an instance of Model Model.find(:first, :conditions => ...) #=> returns an instance of Model Model.find(:all, :conditions => ...) #=> returns an array of Model instances since the result of find(:all) is just an array you can use any method an Array has on the result. http://www.rubycentral.com/ref/ref_c_array.html -- Posted via http://www.ruby-forum.com/.
Alex Wayne wrote:>> ok, so after i did a @all_record.search_param(@params[:query]), i also >> need to find out how many records there are in the @all_record for me to >> use the Paginator, but one thing that went wrong is that maybe i am not >> sure wat kind of variable has @all_record turned out to be. > > Model.find(1) #=> returns an instance of Model > Model.find(:first, :conditions => ...) #=> returns an instance of Model > Model.find(:all, :conditions => ...) #=> returns an array of Model > instances > > since the result of find(:all) is just an array you can use any method > an Array has on the result. > http://www.rubycentral.com/ref/ref_c_array.htmlwell, i tried the .size method but it dont work.... does that means it is not an array ?? -- Posted via http://www.ruby-forum.com/.
bao lee wrote:> Alex Wayne wrote: >>> ok, so after i did a @all_record.search_param(@params[:query]), i also >>> need to find out how many records there are in the @all_record for me to >>> use the Paginator, but one thing that went wrong is that maybe i am not >>> sure wat kind of variable has @all_record turned out to be. >> >> Model.find(1) #=> returns an instance of Model >> Model.find(:first, :conditions => ...) #=> returns an instance of Model >> Model.find(:all, :conditions => ...) #=> returns an array of Model >> instances >> >> since the result of find(:all) is just an array you can use any method >> an Array has on the result. >> http://www.rubycentral.com/ref/ref_c_array.html > > well, i tried the .size method but it dont work.... does that means it > is not an array ??What is the exact errr message? Saying "it dont work" could mena a million things. -- Posted via http://www.ruby-forum.com/.
Alex Wayne wrote:> bao lee wrote: >> Alex Wayne wrote: >>>> ok, so after i did a @all_record.search_param(@params[:query]), i also >>>> need to find out how many records there are in the @all_record for me to >>>> use the Paginator, but one thing that went wrong is that maybe i am not >>>> sure wat kind of variable has @all_record turned out to be. >>> >>> Model.find(1) #=> returns an instance of Model >>> Model.find(:first, :conditions => ...) #=> returns an instance of Model >>> Model.find(:all, :conditions => ...) #=> returns an array of Model >>> instances >>> >>> since the result of find(:all) is just an array you can use any method >>> an Array has on the result. >>> http://www.rubycentral.com/ref/ref_c_array.html >> >> well, i tried the .size method but it dont work.... does that means it >> is not an array ?? > > What is the exact errr message? Saying "it dont work" could mena a > million things.sorry for my hasty post that i forgot about posting the details! the error was ''undefined method ''size'' ''... i tried to do a manual count inside the model itself and when i tried something like self.each, it also result in a ''undefined method ''each'' '' eg. def count_record count_record = 0 self.each do |record| count_record++; end end tahnks alex for pointing out... -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Apr-28 03:46 UTC
[Rails] Re: Re: how to do a count with a variable...
On Apr 27, 2006, at 6:00 PM, bao lee wrote:> Alex Wayne wrote: >> bao lee wrote: >>> Alex Wayne wrote: >>>>> ok, so after i did a @all_record.search_param(@params[:query]), >>>>> i also >>>>> need to find out how many records there are in the @all_record >>>>> for me to >>>>> use the Paginator, but one thing that went wrong is that maybe >>>>> i am not >>>>> sure wat kind of variable has @all_record turned out to be. >>>> >>>> Model.find(1) #=> returns an instance >>>> of Model >>>> Model.find(:first, :conditions => ...) #=> returns an instance >>>> of Model >>>> Model.find(:all, :conditions => ...) #=> returns an array of >>>> Model >>>> instances >>>> >>>> since the result of find(:all) is just an array you can use any >>>> method >>>> an Array has on the result. >>>> http://www.rubycentral.com/ref/ref_c_array.html >>> >>> well, i tried the .size method but it dont work.... does that >>> means it >>> is not an array ?? >> >> What is the exact errr message? Saying "it dont work" could mena a >> million things. > > sorry for my hasty post that i forgot about posting the details! > the error was ''undefined method ''size'' ''... > > i tried to do a manual count inside the model itself and when i tried > something like self.each, it also result in a ''undefined method > ''each'' '' > > eg. > def count_record > count_record = 0 > self.each do |record| > count_record++; > end > end > > tahnks alex for pointing out...You cannot do var++ in ruby. you have to do var += 1 -Ezra
Also when your using the self variable, you are refering to 1 instance of your model again. Not an array, there is no such method as each unless you defind it On 4/28/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:> > > On Apr 27, 2006, at 6:00 PM, bao lee wrote: > > > Alex Wayne wrote: > >> bao lee wrote: > >>> Alex Wayne wrote: > >>>>> ok, so after i did a @all_record.search_param(@params[:query]), > >>>>> i also > >>>>> need to find out how many records there are in the @all_record > >>>>> for me to > >>>>> use the Paginator, but one thing that went wrong is that maybe > >>>>> i am not > >>>>> sure wat kind of variable has @all_record turned out to be. > >>>> > >>>> Model.find(1) #=> returns an instance > >>>> of Model > >>>> Model.find(:first, :conditions => ...) #=> returns an instance > >>>> of Model > >>>> Model.find(:all, :conditions => ...) #=> returns an array of > >>>> Model > >>>> instances > >>>> > >>>> since the result of find(:all) is just an array you can use any > >>>> method > >>>> an Array has on the result. > >>>> http://www.rubycentral.com/ref/ref_c_array.html > >>> > >>> well, i tried the .size method but it dont work.... does that > >>> means it > >>> is not an array ?? > >> > >> What is the exact errr message? Saying "it dont work" could mena a > >> million things. > > > > sorry for my hasty post that i forgot about posting the details! > > the error was ''undefined method ''size'' ''... > > > > i tried to do a manual count inside the model itself and when i tried > > something like self.each, it also result in a ''undefined method > > ''each'' '' > > > > eg. > > def count_record > > count_record = 0 > > self.each do |record| > > count_record++; > > end > > end > > > > tahnks alex for pointing out... > > > You cannot do var++ in ruby. you have to do var += 1 > > -Ezra > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060428/c6d1b7e9/attachment.html
Daniel ----- wrote:> Also when your using the self variable, you are refering to 1 instance > of > your model again. Not an array, there is no such method as each unless > you > defind itthink i solved it le...something wrong with my search statement.... cos it doesnt return the array from the function ... T-T sorry everyone for wasting so much of your time.... -- Posted via http://www.ruby-forum.com/.