On Monday, September 22, 2014 1:49:04 PM UTC-7, Jeremy Evans wrote:
>
> Sequel doesn't currently have a plugin for ActiveRecord::Store like 
> functionality.  Personally, I think it's just a bad idea.  If you are using 
> PostgreSQL, my recommendation would be to use a native json column with 
> Sequel's pg_json extension, and instead of using attr_accessor, just add 
> instance methods that get/set the value in the json hash:
>

I should point out that if you aren't using PostgreSQL, you can use the 
serialization plugin as you are doing currently, and the code below will 
work.  Basically, your issue is you are keeping track of the data in two 
separate places (instance variables and the deserialized json hash), 
instead of a single place.  If you just keep track of it in a single place, 
as the code below does, you are fine, assuming you aren't actually 
modifying attr1 or attr2 during the call to save.

Thanks,
Jeremy
 

>
> class Example < Sequel::Model
>   def attr1
>     data['attr1']
>   end
>
>   def attr1=(v)
>     modified!(:data)
>     data['attr1'] = v
>   end
>
>   def attr2
>     data['attr2']
>   end
>
>   def attr2=(v)
>     modified!(:data)
>     data['attr2'] = v
>   end
>
>   def validate
>     validates_presence [:attr1, :attr2]
>   end
> end
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to