I originally posted this to the Rails GitHub issue tracker (my apologies) here: https://github.com/rails/rails/issues/23490. I'm now posting it in the correct place.
I am trying to mutate objects in memory without persisting them to the database (we always mutate the object, then we use it for a few business purposes, and then we only sometimes save the change, other times we reject them). Active record already supports modifying existing records in memory and adding new records in memory, which only get persisted to the database on save. However, deletions seem to always happen immediately. I believe this is the same idea that was put forth on this issue: #6994
The simplest example I can extract from my code would be something like this:
class CommunicationSetting < ActiveRecord::Base has_one :trigger, autosave: true end class Trigger < ActiveRecord::Base belongs_to :communication_setting, touch: true end c = CommunicationSetting.find(1) c.trigger # => #<TriggerA:0x007faeba3dfa70 id: 1, communication_setting_id: 1> # The following line runs a DELETE query on the database c.trigger.delete # or destroy # I want the delete query to run here, in case I decide to discard the changes c.save!
I have also tried the mark_for_destruction method. That works exactly as I would like for the SQL queries. However, the object acts as if the value still exists, so it's not in a consistent state.
c = CommunicationSetting.find(1) c.trigger # => #<TriggerA:0x007faeba3dfa70 id: 1, communication_setting_id: 1> # No DELETE is run here. Yay! c.trigger.mark_for_destruction # Unfortunately, the trigger still acts as if it is still here c.trigger # => #<TriggerA:0x007faeba3dfa70 id: 1, communication_setting_id: 1> # The DELETE query does run here like I want c.save!
A similar occurrence happens on has_many associations, but it was simpler to demonstrate on the has_one because we don't have to think about the array at the same time.
Another point of reference. This guy on Stack Overflow did a good job explaining the situation:http://stackoverflow.com/questions/11353582/delete-associated-records-when-model-is-saved
I can vouch that the behavior described occurs on Rails version 4.2.5, which is what I'm running in our project.
Can you share some real world examples showing why you'd need to be able to soft delete the associated records? There might be other, better workarounds.
My guess is that this would be a complicated feature to add, but I'm not too familiar with the parts of ActiveRecord that this would touch.
In our specific project we have an object called CommunicationSetting that defines an automated email that a client is setting up. That setting has many different child objects, Filters for example, which would exclude or include people from the recipient list. In this example we would like the client to be able to test how changes to their filters will affect the recipient list. So we would like to take their proposed changes (which might include additions, modification, and deletions), modify the objects in memory, get the list and return it to the UI so the user can review it. If the user likes the changes, they can hit save to persist the object to the database, or if they don’t like it the can abandon their changes and leave the objects in the database as it.
If deletions could be deferred until save time, than running these types of experiments become very trivial. The transaction workaround is plausible, but the whole point of having ActiveRecord objects in memory is the ability to modify them without persisting. Right now the behavior is inconsistent. Additions and modification to objects in a relation are performed in memory only, while deletions are immediately persisted to the database. I think that the inconsistency in behavior is the biggest problem.
If I had something like this:
c = CommunicationSetting.find(1)
the_filters = c.filters # => [#<Filter:0x007f9abe7c3408>, #<Filter:0x007f9abe7b2b30>]
Then I changed the_filters modifying one, removing one, and adding a new one, then executed
c.filters = the_filters
The modification and addition would be in memory only, while the deletion is persisted to the database immediately. This seems very inconsistent and counter intuitive.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
On Jul 7, 2016, at 6:29 PM, Jeremy Mickelson <jeremy.m...@gmail.com> wrote:I completely agree that it would a drastic change, very much not backwards compatible. So something like this should not be taken lightly. I just thought it was worth opening a dialogue because it seems to me that there is a big self inconsistency with the way AR deals with in memory objects. Attributes are modified only in memory until you call save, but associations are saved to the database immediately (there is no way to do an in memory change). That inconsistency cost our small company (3 developers) nearly a hundred hours of head scratching to get around (which is very expensive for a company of our size).I propose that as part of Rails 6, we change the way association modifications are handle to have them only be in memory until save is called.
From the perspective of my use case, I would expect none of the records to save. I have a CommunicationSetting that is treated as one big object from the perspective of the app, but it’s storage is split across multiple AR Models and tables. So what I am doing is modifying a communication setting and its sub records, then calling .save! on the communication setting. If any of the validations failed I would want nothing to save and none of the after save callbacks to fire.
I can see how this could get complicated, but it seems like it could be manageable by looking at which object originally received the save call and treating the whole thing like one atomic operation. In fact isn’t this the way it currently works for modifications (as opposed to deletes), as long as you have autosave: true enabled on the relation. If I have a parent object and I modify both it and some of it’s child records, what happens if some of the validations fail? This is how I currently have my CommunicationSetting class set up and it works great, for everything except deletions.
I would be totally cool with an “add/destroy later” feature that I could enable on the relation because that would allow me to configure this meta record to work the way I want. If we did it that way it wouldn’t even be backward incompatible and wouldn’t have to wait for Rails 6.
--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/83e0K_eAYjU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-co...@googlegroups.com.