Forum: Ruby on Rails Problem regarding accessing attributes from object...

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.
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-05-12 08:35
Hi,
I have an object (@cnt).
Now when I am calling "@cnt.inspect" on object it gives me output as
follows:-
----output----
[#"3"}>]
-----------

Now in aboove output the number between double quotes (3) which is of my
interest & I want to display it. How to do that?
Actually this is my method:-
====================================
class Tagging < ActiveRecord::Base

 def self.find_count_of_tag(tag_name)
     find_by_sql([
            "SELECT count(*) as cntnew FROM taggings " +
            "WHERE taggings.tag_id = (SELECT tags.id from tags where
tags.name=
             'food')"
          ])
  end
end
====================================

Now this is code in my view:-
@cnt=@cnt=Tagging.find_count_of_tag('food')

Now how to get the value of count which is returned by method with SQL
??

PLease help me.
Thanx in advance.
Prash
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-05-12 09:12
(Received via mailing list)
Now that you have @cnt, try @cnt.cntnew. Since that is what you are
calling
the attribue as you defined in your AS part of your SQL query.
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-05-12 10:35
Hi,
I tried with @cnt.cntnew bjt getting error as:-
===============
undefined method `cntnew' for [#<Tagging:0x359bbc8
@attributes={"cntnew"=>"3"}>]:Array
===============

Why I am getting this error n how 2 correct it?
Thanx,
Prash

Ryan Prins wrote:
> Now that you have @cnt, try @cnt.cntnew. Since that is what you are
> calling
> the attribue as you defined in your AS part of your SQL query.
This topic is locked and can not be replied to.