Brent Brent
2007-Oct-02 20:48 UTC
Trying to print on the console a single column from a db
Trying to get a grasp on how ruby works with data, so used to perl.
Here is what im trying to do in my app/models scrap.rb
def self.START
all = MyDb.find_all_by_server_name(''myserver'', :limit
=> 1)
puts all # all i get back here is the address <MyDb:0x9c56058>
puts all[1] # i want element 1 but i get "nil"
end
I have a single table that has a server_name field and it has data in
it as
myserver
Just trying to see how i capture the elements of a row one by one and
access them. From the irb i know can say find :all and it returns the
row but i how do pick this data and assign the values to a usable
variable?. Thanks
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Oct-02 20:58 UTC
Re: Trying to print on the console a single column from a db
Since you are using :limit => 1 you are not going to get an array, just
a single object. Remember that in ruby, the @ signifies an instance
variable, and does not signify that the variable holds an array as it
does in perl. This will work:
def self.START
all = MyDb.find_all_by_server_name(''myserver'', :limit
=> 1)
puts all[:server_name]
end
Brent Brent wrote:> Trying to get a grasp on how ruby works with data, so used to perl.
> Here is what im trying to do in my app/models scrap.rb
>
>
> def self.START
>
> all = MyDb.find_all_by_server_name(''myserver'', :limit
=> 1)
>
> puts all # all i get back here is the address <MyDb:0x9c56058>
>
> puts all[1] # i want element 1 but i get "nil"
>
> end
>
>
> I have a single table that has a server_name field and it has data in
> it as
> myserver
>
> Just trying to see how i capture the elements of a row one by one and
> access them. From the irb i know can say find :all and it returns the
> row but i how do pick this data and assign the values to a usable
> variable?. Thanks
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Oct-02 21:04 UTC
Re: Trying to print on the console a single column from a db
Sorry, my bad, when I first looked at this I read it as @all, but the solution is the same, you are getting a single object rather than an array. Hope this helps. William Pratt wrote:> Since you are using :limit => 1 you are not going to get an array, just > a single object. Remember that in ruby, the @ signifies an instance > variable, and does not signify that the variable holds an array as it > does in perl. This will work: > > def self.START > > all = MyDb.find_all_by_server_name(''myserver'', :limit => 1) > > puts all[:server_name] > > end > > > Brent Brent wrote: > >> Trying to get a grasp on how ruby works with data, so used to perl. >> Here is what im trying to do in my app/models scrap.rb >> >> >> def self.START >> >> all = MyDb.find_all_by_server_name(''myserver'', :limit => 1) >> >> puts all # all i get back here is the address <MyDb:0x9c56058> >> >> puts all[1] # i want element 1 but i get "nil" >> >> end >> >> >> I have a single table that has a server_name field and it has data in >> it as >> myserver >> >> Just trying to see how i capture the elements of a row one by one and >> access them. From the irb i know can say find :all and it returns the >> row but i how do pick this data and assign the values to a usable >> variable?. Thanks >> >> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Brent Brent
2007-Oct-02 21:17 UTC
Re: Trying to print on the console a single column from a db
William Pratt wrote:> Sorry, my bad, when I first looked at this I read it as @all, but the > solution is the same, you are getting a single object rather than an > array. Hope this helps.Thanks william, im just using streamlined framework so i wasnt sure if i was doing something wrong with it. Just need to build a command list and run system on it so its gets executed on the unix side. This is a little bit of a learning curve for me so thanks again for your help -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Oct-02 21:18 UTC
Re: Trying to print on the console a single column from a db
btw, you can also use accessor methods like:
def self.START
all = MyDb.find_all_by_server_name(''myserver'', :limit
=> 1)
puts all.server_name
end
William Pratt wrote:> Sorry, my bad, when I first looked at this I read it as @all, but the
> solution is the same, you are getting a single object rather than an
> array. Hope this helps.
>
> William Pratt wrote:
>> Since you are using :limit => 1 you are not going to get an array,
just
>> a single object. Remember that in ruby, the @ signifies an instance
>> variable, and does not signify that the variable holds an array as it
>> does in perl. This will work:
>>
>> def self.START
>>
>> all = MyDb.find_all_by_server_name(''myserver'',
:limit => 1)
>>
>> puts all[:server_name]
>>
>> end
>>
>>
>> Brent Brent wrote:
>>
>>> Trying to get a grasp on how ruby works with data, so used to perl.
>>> Here is what im trying to do in my app/models scrap.rb
>>>
>>>
>>> def self.START
>>>
>>> all =
MyDb.find_all_by_server_name(''myserver'', :limit => 1)
>>>
>>> puts all # all i get back here is the address
<MyDb:0x9c56058>
>>>
>>> puts all[1] # i want element 1 but i get "nil"
>>>
>>> end
>>>
>>>
>>> I have a single table that has a server_name field and it has data
in
>>> it as
>>> myserver
>>>
>>> Just trying to see how i capture the elements of a row one by one
and
>>> access them. From the irb i know can say find :all and it returns
the
>>> row but i how do pick this data and assign the values to a usable
>>> variable?. Thanks
>>>
>>>
>>
>>
>>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Bob Showalter
2007-Oct-03 02:04 UTC
Re: Trying to print on the console a single column from a db
On 10/2/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> > Since you are using :limit => 1 you are not going to get an array, just > a single object.Huh? That''s not the behavior I get. I get an array with one element if I use find(:all) or find_all_by_blah, with :limit => 1 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter
2007-Oct-03 02:09 UTC
Re: Trying to print on the console a single column from a db
On 10/2/07, Brent Brent <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Trying to get a grasp on how ruby works with data, so used to perl. > Here is what im trying to do in my app/models scrap.rb > > > def self.START > > all = MyDb.find_all_by_server_name(''myserver'', :limit => 1) > > puts all # all i get back here is the address <MyDb:0x9c56058>use inspect to get a better view of what you have (it''s sorta like Data::Dumper): puts all.inspect> > puts all[1] # i want element 1 but i get "nil" > > endall[1] is the *second* element of the array. all[0] or all.first would be the first. Also, when you use find_all, you will get an empty array if the query doesn''t find any matches. If you just want the first match, use find_by_server_name (no "all" and no :limit). That returns either a single object (if a row is found), or nil if nothing was found. row = MyModel.find_by_server_name ''myserver'' puts row.inspect HTH --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Oct-03 05:39 UTC
Re: Trying to print on the console a single column from a db
You are right. First off, I have never tried using an find_all_by_foo method with a :limit of one, but based on the output he was seeing, it looked like a single object, although looking at it again, it is a single element array. Sorry for the confusion. Bob Showalter wrote:> On 10/2/07, William Pratt <billp-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote: > >> Since you are using :limit => 1 you are not going to get an array, just >> a single object. >> > > Huh? That''s not the behavior I get. I get an array with one element if > I use find(:all) or find_all_by_blah, with :limit => 1 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---