vendredi 1 décembre 2017

Rails 3 serialized hash values saving but not persisting

I have a model with a serialized text column. When I save and reload the record, the values seem to persist but when I query the record the values are gone.

(Demonstration below truncated for simplicity)

class Subscription < ActiveRecord::Base
   serialize :pending_changes, Hash
   attr_accessible :pending_changes
   attr_accessor :pending_changes
end

class AddPendingChangesToSubscriptions < ActiveRecord::Migration
  def change
    add_column :subscriptions, :pending_changes, :text
  end
end

s = Subscription.new

s.pending_changes = {foo: "bar"}

s.save 
# => true

s.reload.pending_changes
# => {foo: "bar"}

Subscription.last.pending_changes
# => nil

As per the answers to similar questions, I've made sure the db column datatype is text and I've tried using serialize :column_name with and without the appended Hash.

Aucun commentaire:

Enregistrer un commentaire