On Wednesday, October 10, 2018 at 2:39:20 PM UTC-7, Mike Pastore wrote:
>
> On Wednesday, October 10, 2018 at 1:52:24 PM UTC-5, Jeremy Evans wrote:
>>
>> The simplest solution is just to get the sequence number before inserting:
>>
>> def before_create
>> super
>> self.order_number = DB.get{nextval("bs_order_number_seq")}
>> end
>>
>
> For posterity, here is the solution I ended up with:
>
> def before_create
> super
> self.number ||= Sequel.function(:nextval, Sequel.function(:
> pg_get_serial_sequence, self.class.table_name.to_s, 'order_number'))
> end
>
> This lets me specify an order number if desired, avoids a round trip to
> the database, and doesn't require a pre-determined index name.
>
Technically, that's not kosher. Model instance values should be values and
not expressions, and an SQL function is an expression. You sort of get
away with it here since validation is run before the hooks and you probably
aren't running with strict param setting turned on.
I think the issue can be avoided completely by modifying the
defaults_setter plugin to not cache values if there is no default value.
Try the master
branch:
https://github.com/jeremyevans/sequel/commit/8b6f117ff47ff8c28f0b952cc20a07300b9f2ecb
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.