All threads / ActiveSupport Concerns: Making a Soft Deletable module Discussion

Ask A Question

Notifications

You’re not receiving notifications from this thread.

ActiveSupport Concerns: Making a Soft Deletable module Discussion

You could add :deleted_at field inside included_do, so it's added everywhere you include the concern. Actually not sure if it's possible with ActiveRecord, but with Mongoid for sure.

I don't think you can with ActiveRecord since you need a database migration to add it. That's a great point for MongoDB users though! Super simple.

One of the reasons to work with Mongo :-)

Reply
Chris Haack ·

If you want to hit callbacks like after_destroy you can wrap your code in a run_callbacks block like so...

https://gist.github.com/Chr...

see http://apidock.com/rails/Ac...

Ah yes! I'll have to do an episode on that too.

Chris Haack ·

Cool!

Reply

Love unraveling the magic of some of these really useful gems :)

Reply
Ryan Bigg ·

TIL: you did a screencast on one of my gems :D

I did! Thanks for your work on paranoia! :)

Reply

Excellent Episode, my friend. Thank you!

Reply

Great job! These are really interesting, and elegent. They are kind of software engineering problems. Makes you think! Can you do more like this? :-)

Absolutely! I like trying to show how to use a gem as well as showing how you could build your own version from scratch. It makes using any library a lot less daunting when you know you could go in and fix it yourself if you wanted. :)

Got any suggestions you'd like to see?

Reply

I'm building my own soft deletable module, but I would like to set the current_user.id in a deleted_by column, to know which user deleted it. It is bad practice to set the user as a global variable available to the model layer due to thread safety...see thread_mattr_accessor and thread_cattr_accessor and a nice article by BigBinary. I will simply implement a destroy(user_id=0) method. If the user_id is passed through from the controller, it will be saved...otherwise the deleter will be 0 and therefore unknown.

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.