You can use BaseClass.sti_class_from_sti_key(type).create

Hope this helps


> 12. 2. 2021 v 17:08, 'Michael Monerau' via sequel-talk 
> <[email protected]>:
> 
> 
> Ah clever!
> 
> On a related note, I have struggled in the past to `create` a new model. I 
> would have liked that it is possible to call `BaseClass.create` and the 
> actually created class is the correct precise class given on the STI key 
> value. It is important when serialization occurs depending on the 
> instantiated class for example.
> 
> Adding a `create_precise` method that would do the lookup would also be 
> acceptable.
> 
> Is there a clever way to achieve that (without looking up the correct class 
> myself at creation site), or would you consider adding such a feature to the 
> plugin?
> 
> Best,
> Michael
> 
>  
>  
> 
> 
> Le ven. 12 févr. 2021 à 16:52, Jeremy Evans <[email protected]> a écrit :
>> On Fri, Feb 12, 2021 at 4:34 AM '[email protected]' via sequel-talk 
>> <[email protected]> wrote:
>>> Hello,
>>> I'd like to disable a possibility for creating a model directly from STI 
>>> base class. Any idea how to do that properly?
>>> 
>>> Maybe you're asking why I need that. Imagine that there are some 
>>> validations defined in subclasses but if you create model directly from a 
>>> base class, then those validations are not performed...
>> 
>> Something like this should work:
>> 
>> class BaseClass < Sequel::Model
>>   def before_create
>>     raise if instance_of?(BaseClass)
>>     super
>>   end
>> end 
>> 
>> 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 view this discussion on the web visit 
>> https://groups.google.com/d/msgid/sequel-talk/CADGZSSfDoGbVF5G7Jd%2BPxhc4H25%2ByU_JQ9N8PAo0kUGL82HnhA%40mail.gmail.com.
> 
> -- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "sequel-talk" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/sequel-talk/xnKCdDek0Qo/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to 
> [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sequel-talk/CAAkfp_QgQ6gpeeG%2BwJpgitbyzyCJ6%2BK7C_hT-LUYBQFXATyFYg%40mail.gmail.com.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/0738AC78-F435-4A9D-BC15-CC87CB772B30%40me.com.

Reply via email to