Forum: Ruby on Rails how to do a count with a variable...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4a021d3e4528fb8ab78ef7fc4b6822cc?d=identicon&s=25 Bao Lee (burninglegion)
on 2006-04-27 06:05
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
8a456610e31c8feadc4cef71d9347cbd?d=identicon&s=25 Craig Ambrose (Guest)
on 2006-04-27 06:21
(Received via mailing list)
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
4a021d3e4528fb8ab78ef7fc4b6822cc?d=identicon&s=25 Bao Lee (burninglegion)
on 2006-04-27 06:36
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???
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-04-27 07:10
(Received via mailing list)
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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-27 08:23
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?
4a021d3e4528fb8ab78ef7fc4b6822cc?d=identicon&s=25 Bao Lee (burninglegion)
on 2006-04-27 08:28
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...
>
> -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. :)

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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-27 08:54
> 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
4a021d3e4528fb8ab78ef7fc4b6822cc?d=identicon&s=25 Bao Lee (burninglegion)
on 2006-04-27 09:16
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 ??
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-27 18:07
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.
4a021d3e4528fb8ab78ef7fc4b6822cc?d=identicon&s=25 Bao Lee (burninglegion)
on 2006-04-28 03:00
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...
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-28 05:48
(Received via mailing list)
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-04-28 06:49
(Received via mailing list)
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
4a021d3e4528fb8ab78ef7fc4b6822cc?d=identicon&s=25 Bao Lee (burninglegion)
on 2006-04-28 08:41
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....
This topic is locked and can not be replied to.