Comments (6)
@ivanbrennan I think now you can do big.littles.discard_all
, which is way more explicit what is going on. Also, I think that sometimes you would actually want to just null out associations rather than discarding them.
from discard.
Thanks for your clarification @kowal.
@ivanbrennan If you're looking for a gem that does soft deletion by overriding ActiveRecord behaviour, you might like paranoia.
I may make this more clear in the project's README or issue/pr templates, but discard does not and will not ever override the behaviour of ActiveRecord methods. That's the main thing that distinguishes it from paranoia.
from discard.
big.littles = []
doesn't normally delete records, so it doesn't make sense for it to discard records either. We're going to leave it as is and keep the existing Rails behaviour the same, as changing this would be pretty unexpected.
from discard.
big.littles = []
doesn't normally delete records
@jarednorman That has not been my experience, and the has_many
documentation seem to confirm the delete behavior:
collection=objects
Replaces the collections content by deleting and adding objects as appropriate.
@kowal Using discard_all
would make sense if I was unconditionally discarding, but the use-case that brought this up for me was handling an update that modifies the associated collection, adding some new records, potentially modifying some existing ones, and potentially removing some existing ones.
from discard.
@ivanbrennan @jarednorman
OK, It's not so simple. From what I checked, having has_many
association without any options does not destroy/delete associated records when calling big.littles = []
. But this indeed happens when I specify dependent: :destroy
option.
So, since big.littles = []
does not have single semantic and it depends on association config, maybe it is better not to override it with discard function.
from discard.
@kowal @jarednorman Thank you both for the clarifications. Indeed, in my actual use-case I was using dependent: :destroy
. (I should have included that in my example, apologies.)
from discard.
Related Issues (20)
- Bang-Methods hides internal errors (e.g. ActiveModel::ValidationError) HOT 1
- Discarded scope doesnt work properly HOT 1
- discard has_and_belongs_to_many association HOT 1
- Discard callbacks don't work with observers HOT 3
- NoMethodError (undefined method `undiscarded?' ... HOT 4
- is discard_all! working on version 1.1.0 HOT 2
- after_discard_commit HOT 1
- All new records are discarded use-case HOT 2
- Run callbacks inside of the same transaction HOT 5
- Rails 7 is here, why not drop the upper version dependency? HOT 1
- Support for Rails 7.0.0 HOT 2
- Add the ability to use `dependent: :discard` in associations HOT 1
- Nil when finding has_one association HOT 2
- how is the integration with papertrail? HOT 1
- dependent: :discard HOT 12
- Sequel Support HOT 1
- guard on discard HOT 1
- `self.class.discard_column` as a boolean `is_discarded` for "if" and use `updated_at` for "when" HOT 1
- `def undiscard` doesn't follow `return false` pattern HOT 1
- .discarded query HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from discard.