How to do a count with a variable


#1

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


#2

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


#3

Craig A. 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???


#4

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…

-Brian


#5

bao lee wrote:

Craig A. 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?


#6

Brian H. 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…

-Brian

sorry 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. :slight_smile:

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


#7

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


#8

Alex W. 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 ??


#9

Alex W. wrote:

bao lee wrote:

Alex W. 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…


#10

On Apr 27, 2006, at 6:00 PM, bao lee wrote:

an Array has on the result.
the error was 'undefined method ‘size’ '…
end
end

tahnks alex for pointing out…

You cannot do var++ in ruby. you have to do var += 1

-Ezra


#11

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


#12

bao lee wrote:

Alex W. 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.


#13

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 it

think 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…