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
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
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.
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 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
On 10/2/07, William P. [email protected] 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 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.
On 10/2/07, Brent B. [email protected] 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