# Working with a Legacy DB Question

**URL:** <https://www.ruby-forum.com/t/working-with-a-legacy-db-question/140809>\
**Category:** Rails\
**Created:** [June 18, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/working-with-a-legacy-db-question/140809 "2008-06-18T20:04:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 18, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/working-with-a-legacy-db-question/140809/1 "2008-06-18T20:04:03Z")

</div>

Hello All,  
My question is this.  
I’m trying to connect to a MySQL DB for read only in ROR

Class Report \< ActiveRecord::Base  
set\_table\_name “stories”  
set\_primary\_key “record\_id”  
end

script/console

story = Report.find(56734) #Retrieve just a specific record\_id Works  
but I’m not able to iterate over this.

story.each { |fields| puts fields }  
NoMethodError: undefined method `each' for #<Editorial:0x4979ce0> from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ attribute_methods.rb:256:in`method\_missing’  
from (irb):226  
from :0

So assuming there is another method that I can use to gather all or  
selected fields?

If I reference each filed by name it works  
story.record\_id  
story.file\_name

I’m sure I’m missing something very basic here.  
Thanks Sc

---

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 18, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/working-with-a-legacy-db-question/140809/2 "2008-06-18T20:05:15Z")

</div>

On Jun 18, 6:51 pm, scottc12 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So assuming there is another method that I can use to gather all or  
> selected fields?

If you want to iterate over all attributes, look at story.attributes.

Fred

---

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 18, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/working-with-a-legacy-db-question/140809/3 "2008-06-18T21:31:23Z")

</div>

On Jun 18, 2008, at 1:51 PM, scottc12 wrote:

> from :0  
> Report.find(56734) will return a single Report object  
> Report.find([56734]) will return an array containing a single Report  
> object.

Is that the iteration that you want?

> So assuming there is another method that I can use to gather all or  
> selected fields?
> 
> If I reference each filed by name it works  
> story.record\_id  
> story.file\_name
> 
> I’m sure I’m missing something very basic here.  
> Thanks Sc

Otherwise, if you want the fields or attributes of a single Report  
(one row from stories and its columns), then Fred has already given  
you the hint.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![scottc12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71c47a/32.png) [@scottc12](https://www.ruby-forum.com/u/scottc12)\
**Post date:** [June 18, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/working-with-a-legacy-db-question/140809/4 "2008-06-18T23:41:37Z")

</div>

Hello All,

Thanks so much this has put me on the correct track. I was not sure  
how to get things into an array which this solves. I suppose I could  
also do a Stories.find(:all) and which returns and array.

Thanks everyone …

sc

On Jun 18, 3:30 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
