【发布时间】:2011-09-15 19:12:02
【问题描述】:
我无法让inverse_of 在创建时加入模型。我不确定这是一个错误还是没有这样实现。我有以下型号:
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
contact_ids 是我的challenge 的虚拟属性,我想在创建该关联时在group_challenges 模型中访问这些属性。我无法让它工作。这是一个例子:
challenge = Challenge.new :groups => Group.all, :contact_ids => [1,2,3]
# log output => challenge.contact_ids: []
但是inverse_of 在重新加载模型时确实有效
challenge.reload
challenge.group_challenges.first.challenge.contact_ids
# log output => challenge.contact_ids: [1,2,3]
有谁知道这只是 inverse_of 的设计限制还是实现中的错误?
【问题讨论】:
-
我也有同样的问题。
标签: ruby-on-rails activerecord associations