I've used something similar in the past for an API that had very specific 
return codes, i.e. 201 if the resource was created and 200 or 204 if the 
resource was updated. 

I'm working on an app now that allows folks to subscribe to create and/or 
update events, so I need to know whether a resource was created or updated. 
Sometimes it gets a little hairy and I have to do something like this:

is_new = false
my_foo = Foo.find_or_create(:key=>value) { is_new = true }
# other logic that requires my_foo to exist in the database e.g. creating 
or linking associated records

In this particular case I actually have a "placeholder" flag on the record 
and the resource isn't considered created if the record is a placeholder:

my_foo = Foo.find_or_create(:key=>value) { |o| o.placeholder = true }
# other logic that requires my_foo to exist in the database e.g. creating 
or linking associated records
is_new = my_foo.placeholder?
# other logic that might toggle my_foo.placeholder

I'm sure there were other ways to solve this problem, but this is the 
pattern I've gravitated toward in the past. A plugin for this could help, 
but the requirements can vary so much between use cases that I would end up 
needing to roll my own anyway. 

On Thursday, December 30, 2021 at 2:12:14 AM UTC-6 Jeremy Evans wrote:

> On Wed, Dec 29, 2021 at 8:47 AM tr...@upserver24.com <tr...@upserver24.com> 
> wrote:
>
>> Hi,
>>
>> We found this tiny useful plugin that allows to detect if model instance 
>> was just created:
>>
>> ```
>> module Sequel
>>   module Plugins
>>     module JustCreated
>>       module InstanceMethods
>>         def after_create
>>           @just_created = true
>>           super
>>         end
>>
>>         def just_created?
>>           @just_created == true
>>         end
>>       end
>>     end
>>   end
>> end
>> ```
>>
>> I was trying to do it using :dirty plugin but didn't found a way, maybe I 
>> missing something? If not don't you mind if we create PR to include this 
>> plugin into sequel?
>>
>
> I don't think the dirty plugin supports this.  Could you explain the use 
> case for the plugin?  That would help me to better judge whether to include 
> it with Sequel.  Note that even if it isn't included with Sequel, it's easy 
> to ship as a external gem.
>
> The plugin has at least one implementation issue:
>
> Model.new.save.save.just_created? # => true, should be false, assuming you 
> want a object that hasn't been updated
>
> Also, I'm not sure "just_created" is a good name.  After all:
>
> m = Model.create
> sleep(60*60*24)
> m.just_created? # "just" generally implies recently.
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/70677226-2357-4794-8d11-df6c168f5ab3n%40googlegroups.com.

Reply via email to