0

I am storing an object of type Feed into my mysql db as a longblob and want to know how to retrieve it without it being of type String.

Is there a way to typecast in ruby/rails that I'm overlooking?

I'm assuming that's whats happening since I get "undefined method `entries' for #

some background in case I did something stupid...

my migrations:

class AddFeedDataToFeeds < ActiveRecord::Migration
  def change
    add_column :feeds, :feed_data, :longblob
  end
end

class FillFeedData < ActiveRecord::Migration
  def up

    Feed.all.each do |f|
      f.update_attribute(:feed_data, Feedzirra::Feed.fetch_and_parse(f.feed_url))
    end

  end

In my reader controller:

next_feed = Feed.find(session[:unread_random].pop)
    @feed = next_feed

In the reader:

<% unless @feed.is_a? Fixnum #protect against bad feed URLs: 404, 503 etc...
    @feed.entries.each do |entry| %>
    <div class="entry">
        <h1><%= entry.title %></h1>

etc... The error throws in the second line of this last code segment.

Ken W
  • 962
  • 4
  • 12
  • 19
  • Whooooa, its' alive! Longblobs? Crazy. Did you see this ? http://stackoverflow.com/questions/579536/how-do-you-get-rails-to-use-the-longblob-column-in-mysql You might not be making your blob right. – Trip Aug 13 '12 at 23:24
  • Yeah I had seen it, just rolled back my migrations and did it their way, the db shows its using longblob again and I still get the same error. I just tried a puts on @feed and it displays the feed like it should but I need to typecast the string to feedzirra's return type... – Ken W Aug 13 '12 at 23:36

1 Answers1

0

Here's the solution:

I realized that when Rails stores the Feedzirra AtomFeedBurner or RSSFeedBurner (or any of Feedzirra's parsers), into a longblob column, it was storing it as text rather than binary.

I tried several things, Base64's encode64 only accepts strings so that didn't work.

Turns out you need to use Marshal.dump and Marshal.load to serialize and deserialize the object into/out of the blob field to protect original object integrity.

This works for me just fine for now. Eventually I am sure that I will need a more optimized solution.

Also found that to typecast you can use .becomes() but that's not a good idea.

Ken W
  • 962
  • 4
  • 12
  • 19