On Tuesday, December 24, 2019 at 8:20:46 AM UTC-8, Illia Kasianenko wrote:
>
> Hi,
>
> The hypothetical create_or_find method could be a nice alternative to
> transaction { find_or_create ... }.
> It is implemented in AR 6 and the concept is well explained in docs:
> https://apidock.com/rails/v6.0.0/ActiveRecord/Relation/create_or_find_by
> <https://www.google.com/url?q=https%3A%2F%2Fapidock.com%2Frails%2Fv6.0.0%2FActiveRecord%2FRelation%2Fcreate_or_find_by&sa=D&sntz=1&usg=AFQjCNElvBSri-7vwmD7MrmLWov_3f_Q5Q>
> What do you think about that?
>
Should be fairly easy to implement in a plugin if people want the
behavior. Note that create_or_find_by still has a race condition, if the
record is deleted between the create and find. If you want a solution that
avoids race conditions, you need to use database-specific APIs. For
example, on PostgreSQL:
Model.dataset.insert_conflict(:target=>:column_name,
:update=>{:id=>Sequel[:table_name][:id]}).returning.insert(column_name:
value)
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/0aa87a7e-fdb4-4459-9902-4763b4fc71a3%40googlegroups.com.