getting active records to display as a plist

Posted by phil swenson on Stack Overflow See other posts from Stack Overflow or by phil swenson
Published on 2009-08-12T05:18:23Z Indexed on 2010/06/12 19:52 UTC
Read the original article Hit count: 147

Filed under:
|
|

I'm trying to get a list of active record results to display as a plist for being consumed by the iphone. I'm using the plist gem v 3.0.

My model is called Post. And I want Post.all (or any array or Posts) to display correctly as a Plist.

I have it working fine for one Post instance: [http://pastie.org/580902][1]

that is correct, what I would expect. To get that behavior I had to do this:

class Post < ActiveRecord::Base
   def to_plist
     attributes.to_plist
   end
end

However, when I do a Post.all, I can't get it to display what I want. Here is what happens: http://pastie.org/580909

I get marshalling. I want output more like this: [http://pastie.org/580914][2]

I suppose I could just iterate the result set and append the plist strings. But seems ugly, I'm sure there is a more elegant way to do this.

I am rusty on Ruby right now, so the elegant way isn't obvious to me. Seems like I should be able to override ActiveRecord and make result-sets that pull back more than one record take the ActiveRecord::Base to_plist and make another to_plist implementation. In rails, this would go in environment.rb, right?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about activerecord