views:

30

answers:

1

Given a database with records like the following 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

+1  A: 

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.

Chuck Vose
That's incredible,,, bonus point for if you can share how to store the data field like it is above. thanks!
WozPoz
Probably just need to marshall the data before it goes into the database. Check out this link: http://api.rubyonrails.org/classes/ActiveRecord/Base.html specifically the section on saving arrays and hashes. Excellent guide.
Chuck Vose
@Chuck, just tried the above and it errors? does the p in the front have any significance? I tried it w and w/o and both failed... The error I get is "NoMethodError" undefined method `photo_id' for #<String:0x1033a80f0>
WozPoz
Use `result.data['photoID']` to index into a Hash object in Ruby.
Raphomet
@raphomet thanks... I just tried that and it just outputs photoID and not the value in the DB meta field data? I tried it without the ' so just [photoID] and it errors: undefined local variable or method `photoID'
WozPoz
Am I storing it in the database correctly? {photoID:2089, photoName: "blah"}
WozPoz
It looks like you converted a JavaScript Object into a string and saved it. You might have to cast into a Ruby Hash after fetching it out of the database: `JSON.parse(result.data)['photoID']`
Raphomet
So here's the deal... I haven't built the part that stores the data yet, and that won't be JSON based. How should the data look in data field to be a Ruby HASH?
WozPoz
Woz, I apologize but I have to go to bed. @raph, thanks for the correction.
Chuck Vose
Make the database column a string. Marshal (which means to serialize a data object into a string) the data using `Marshal.dump(data)`. When you fetch it back out of the database, you can convert it back into a Hash using `Marshal.load(data)`.
Raphomet
@Raphomet interesting, I have not heard of the ability to Marshal, google searching to learn more and try it out. Not finding much, know of any tutorials or examples so I can understand the suggestion? or would it be possible to see an example Marshal.dump and load,, and what the data would look like for the above in the database for 1 field? thanks
WozPoz
What about YAML?
WozPoz
Marshaling is dangerous, as it may not be interoperable, and it may differ depending on ruby Ruby version. I preffer YAML or JSON - data lasts longer than applications.
gertas
I think I'm going to go with JSON, seems pretty simple to use... Anyone know about the JSON performance?
WozPoz