Coder Social home page Coder Social logo

Comments (6)

kowal avatar kowal commented on September 13, 2024 1

@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.

jarednorman avatar jarednorman commented on September 13, 2024 1

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.

jarednorman avatar jarednorman commented on September 13, 2024

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.

ivanbrennan avatar ivanbrennan commented on September 13, 2024

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.

kowal avatar kowal commented on September 13, 2024

@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.

ivanbrennan avatar ivanbrennan commented on September 13, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.