# Ordered active record attributes hash

**URL:** <https://www.ruby-forum.com/t/ordered-active-record-attributes-hash/192484>\
**Category:** IronRuby\
**Created:** [July 31, 2010, 12:26am UTC](https://www.ruby-forum.com/t/ordered-active-record-attributes-hash/192484 "2010-07-31T00:26:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [July 31, 2010, 12:26am UTC](https://www.ruby-forum.com/t/ordered-active-record-attributes-hash/192484/1 "2010-07-31T00:26:21Z")

</div>

Hi All:

## I’m trying to traverse the active record attributes hash in the order it was created.

a = Table.find\_by\_sql(“select \* from sometable”)

a.each { |row| puts row.to\_yaml

# prints the the attributes in the order coming from the select

statement

# which is exactly how I would like to process the columns

puts row.attributes.to\_yaml

# shows the attributes in sorted order…

}

* * *

Is that a problem with activerecord?

how I could solve the issue?

---

<div class="post-metadata">

**Author:** ![michbukana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@michbukana](https://www.ruby-forum.com/u/michbukana)\
**Post date:** [July 31, 2010, 9:32pm UTC](https://www.ruby-forum.com/t/ordered-active-record-attributes-hash/192484/2 "2010-07-31T21:32:26Z")

</div>

Hi all,

I found the solution for this puzzle:

Active record, when you call the procedure attributes, it sorts the  
attributes by key, so, by working in the following way, that works  
correctly:

row.instance\_variable\_get(:@attributes).to\_yaml

instead of

row.attributes.to\_yaml

Thank you anyways

Eduardo

Eduardo B. wrote:

> Hi All:
> 
> ## I’m trying to traverse the active record attributes hash in the order it was created.
> 
> a = Table.find\_by\_sql(“select \* from sometable”)
> 
> a.each { |row| puts row.to\_yaml
> 
> # prints the the attributes in the order coming from the select
> 
> statement
> 
> # which is exactly how I would like to process the columns
> 
> puts row.attributes.to\_yaml
> 
> # shows the attributes in sorted order…
> 
> }
> 
> * * *
> 
> Is that a problem with activerecord?
> 
> how I could solve the issue?
