user2419316
user2419316

Reputation: 331

How do I construct the proper before_destroy in model in rails

I have the following which deletes a bin and everything related to it. It's great and functions;

MODEL BIN

class Bin < ActiveRecord::Base

    has_many :savedtweets, :dependent => :destroy
    before_destroy :mod_newtweets

    def mod_newtweets
        Newtweet.where(:tweet_id => @bin.savedtweets.pluck(:tweet_id)).update_all(:status => 'new')
    end
end

However, it destroys a bin, deletes everything but doesn't run :mod_newtweets to update my other table and its column.

If I put this in the controller it works fine;

Newtweet.where(:tweet_id => @bin.savedtweets.pluck(:tweet_id)).update_all(:status => 'new')

I thought I've got everything done right.

Upvotes: 0

Views: 63

Answers (1)

Manoj Monga
Manoj Monga

Reputation: 3083

Replace your method with:

def mod_newtweets
    Newtweet.where(:tweet_id => savedtweets.pluck(:tweet_id)).update_all(:status => 'new')
end

You are using @bin.savedtweets in your model while you have not defined @bin anywhere. as it is an instance method you can use either self.savedtweets or only savedtweets to call the savedtweets method on current instance of Bin model.

Upvotes: 1

Related Questions