I have a model that has a number of has_many, and has_many :through model relationships. For example, in my User class I have:
has_many :languages, through: :profile_languages
What I would like is to be able to detect when these are added or removed using the 'User.changes' function, which returns an array of attributes that have been changed when called with the User.language_ids= function.
Has anyone else tried to do this, or have experience with this?
Info on the ActiveModel changes function: http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
Edit: As requested, here is what I am doing.
After a user's attributes are assigned and before it is saved, I am looking at all of the values returned from the .changes, in order to log all the changes made in an external log.
So if I call u.name = "new name"
then u.changes returns {name: ['old name', 'new name']}
However, when I pass a user a bunch of language ids, such as
u.language_ids = [4,5]
Then there are a number of ProfileLanguage models created, and the u.changes hash is left blank.
I am attempting to create some callbacks in the ProfileLanguage model that would manually create some sort of a hash, but I am wondering if this is indeed the best solution.