Bradley
2011-Sep-15 19:35 UTC
ActiveRecord :inverse_of does not work for has_many :through on the join model on create
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 ? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/3UlKGHSyJqwJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.