inverse_of does not work on join models on creation. This is either a bug or just a limitation in the implementation of inverse_of. Here’s an example:
class Challenge < ActiveRecord::Base has_many :groups, :through => :group_challenges has_many :group_challenges, :inverse_of => :challenge
attr_accessor :contact_ids end
class GroupChallenge < ActiveRecord::Base belongs_to :challenge, :inverse_of => :group_challenges belongs_to :group, :inverse_of => :group_challenges
before_save :check_challenge
def check_challenge Rails.logger.debug(“challenge.contact_ids: #{challenge.contact_ids}”) end end
class Group < ActiveRecord::Base has_many :challenges, :through => :group_challenges has_many :group_challenges, :inverse_of => :group end
The GroupChallenge model does not reference the same Challenge when it is created via the associated Group
For example:
challenge = Challenge.new :groups => Group.all, :contact_ids => [1,2,3]
log output from GroupChallenge
=> challenge.contact_ids:
However inverse_of does work when the models are reloaded
challenge.reload challenge.group_challenges.first.challenge.contact_ids
log output => challenge.contact_ids: [1,2,3]
Is this a bug or just a limitation in the implementation of inverse_of ?