# Trying to print on the console a single column from a db

**URL:** <https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148>\
**Category:** Rails\
**Created:** [October 2, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148 "2007-10-02T22:48:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 2, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/1 "2007-10-02T22:48:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 2, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/2 "2007-10-02T22:59:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 2, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/3 "2007-10-02T23:06:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 2, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/4 "2007-10-02T23:19:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 2, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/5 "2007-10-02T23:17:19Z")

</div>

William P. 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

---

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 3, 2007, 4:05am UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/6 "2007-10-03T04:05:22Z")

</div>

On 10/2/07, William P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 3, 2007, 7:39am UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/7 "2007-10-03T07:39:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![newrubyuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/898d66/32.png) [@newrubyuser](https://www.ruby-forum.com/u/newrubyuser)\
**Post date:** [October 3, 2007, 4:10am UTC](https://www.ruby-forum.com/t/trying-to-print-on-the-console-a-single-column-from-a-db/116148/8 "2007-10-03T04:10:03Z")

</div>

On 10/2/07, Brent B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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"
> 
> ```
> 
> end

all[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
