Ordered active record attributes hash

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?

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs