I can't figure out how to check to see if a record is newly saved. right now I am using
If @item.save
@a = Item.where(“restaurant_id = ?”, params[:item][:user_id]).where(“created_at < ?”, 2.minutes.ago)
The query is working fine but I'm wondering if there is a better way to check to see if the record was just created. I was looking into ActiveModel::Dirty but once the record is saved, :created_at_changed? will return false.
Determine whether the current record is a new record on Form Load. Look forward to kind reply. What do you mean by new record? A record that was recently created? You can get the record 'createdon' and get the current date and time then compare. You can also use getFormType. If the form is "Create" = New or "Update" = existing.
The first method is that you can create a Flow in the solution, and then use the When a record is created, updated or deleted included in the Common Data Service (current environment) connector as the trigger, and then check whether there is any in the trigger body that can be used Determine that the current record is created/updated/deleted.
wasRecentlyCreated indicates if the model was inserted during the current request lifecycle. So even if the record was missing from the DB, it will only return true after it is saved.
You can get the record 'createdon' and get the current date and time then compare. You can also use getFormType. If the form is "Create" = New or "Update" = existing. You can refer to this microsoft page for more info.
For the record, Rails 4 and 5 now both have functionality for this.
In Rails 4, you can do:
@item.previous_changes.key?('id')
And in Rails 5:
@item.id_previously_changed?
[NOTE: This assumes you never manually update the IDs of your records. If you are, you'll need something more like the following:]
@item.previous_changes.key?('id') && @item.previous_changes['id'][0].nil?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With