Rails 3 - Looping through DB records, one record has a HASH
Given a database with records like the following开发者_如何学Go Table: AuditLog Fields: ID | USERID | TYPE | TIME | DATA
id:1, userId:1, type:PHOTO, time:2008-10-15 12:00:00, data:{photoId:2089, photoName:A trip to the beach}
Lets say the database had 50 records, with Rails How can I loop through the results as follows:
- id, user_id, data.photoID, data.PhotoName
The main thing I don't get is how to extract what's inside the data column, the has that's inserted int the db field.
Thanks
If you have a list of results like this you would do something like the following:
@results.each do |result|
p result.data.photoId
end
You can certainly get fancier but hopefully this will get you along for now.
精彩评论