相关疑难解决方法(0)

ActiveRecord:inverse_of不适用于has_many:通过on上的连接模型

我无法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
Run Code Online (Sandbox Code Playgroud)

contact_ids是我的虚拟属性challenge,我想在group_challenges创建该关联时在模型中访问这些属性.我无法让它发挥作用.这是一个例子:

challenge = Challenge.new :groups => Group.all, :contact_ids => [1,2,3]
# log output => challenge.contact_ids: []
Run Code Online (Sandbox Code Playgroud)

inverse_of …

activerecord ruby-on-rails associations

6
推荐指数
1
解决办法
4978
查看次数

标签 统计

activerecord ×1

associations ×1

ruby-on-rails ×1