On Friday, July 29, 2016 at 9:35:56 AM UTC-7, [email protected] wrote:
>
> I have a model with a serialized column of data, and I need to make sure 
> that certain fields exist in the serialized data depending on different 
> criteria (I'm using the enum plugin for status)
>
> class Order < Sequel::Model
>   enum :status, [:draft, :submitted, :approved]
>   plugin :serialization
>   serialize_attributes :json, :sales_line
>
>
>   def validate
>     super
>     validates_presence :sales_line unless draft?
>     # How can I validate that the sales lines also have a "sales_qty" 
> attribute?
>   end
>
> end
>
> Basically, inside my serialized data of sales lines I have some fields. 
> Based on the order status I need to make sure that these fields are present:
>
> - "sales_qty" must be filled in unless the order is a draft
> - "approved_qty" must be filled in if the order is approved
>

I would think you would just add the following to validate:

    errors.add(:sales_qty, "Not filled in") if !draft? && && (!sales_line 
|| !sales_line['sales_qty'])
    errors.add(:approve_qty, "Not filled in") if approved? && (!sales_line 
|| !sales_line['approved_qty'])

Assuming you added getter methods for sales_qty and approve_qty, you could 
probably use validates_presence, similar to how you are doing for 
sales_line itself.

Thanks,
Jeremy

-- 
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 https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to