开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜