2011-09-15 21 views
6

Oluşturma oluşturma modellerinde çalışmak için inverse_of alamıyorum. Bunun bir hata mı, yoksa böyle uygulanmadığından emin değilim.ActiveRecord: inverse_of has_many üzerinde çalışmaz: oluşturma modelinde aracılığıyla oluşturma

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 benim challenge sanal bir niteliktir ve bunu dernek oluşturulduğunda group_challenges modelinde bu erişmek istediğiniz: Aşağıdaki modelleri var. İşe yaratamıyorum. İşte bir örnek:

challenge = Challenge.new :groups => Group.all, :contact_ids => [1,2,3] 
# log output => challenge.contact_ids: [] 

Ancak inverse_of yapar modelleri yeniden yüklenir eser

challenge.reload 
challenge.group_challenges.first.challenge.contact_ids 
# log output => challenge.contact_ids: [1,2,3] 
bu uygulamada inverse_of sadece bir tasarım sınırlaması ya da daha doğrusu bir hata olup olmadığını

bilen var mı ? gereğince

+0

Aynı sorunu yaşıyorum. – rafamvc

cevap

17

active record api 3.2.1: "Şu anda:. inverse_of has_one ve has_many (ancak: varyantları aracılığıyla) destekleyen dernekler Ayrıca ters bir has_one olan belongs_to dernekler için ters destek sağlayan ve bir değil polimorfik."

+2

oh iyi bir tane, bu belgeleri hiç fark etmedim. Çok kötü, çok kullanışlı olurdu! – brad

İlgili konular