2011-11-22 65 views
1

我環顧四周,並建立了多態多對多模型似乎工作。我把它就像這樣:多對多多態模型 - 設置工廠女孩

class Category < ActiveRecord::Base 
    has_many :category_categoryable 
    has_many :blogs, :through=>:category_categoryable 
    has_many :language, :through=>:category_categoryable 

class Blog < ActiveRecord::Base 
    has_many :category_categoryable, :as=>:categoryable 
    has_many :category, :through=>:category_categoryable 

class Language < ActiveRecord::Base 
    has_many :category_categoryable, :as=>:categoryable 
    has_many :category, :through=>:category_categoryable 

class CategoryCategoryable < ActiveRecord::Base 
    belongs_to :category 
    belongs_to :blog, :polymorphic=>true 
    belongs_to :language, :polymorphic=>true 

沒有任何理由爲什麼我不應該這樣做,也是我無法工作,如何建立工廠的女孩了,我已經試過這樣:

FactoryGirl.define do 
    factory :blog do 
    sequence(:title) {|b| "Blog name #{b}" } 
    content "blog content" 
    meta "meta content" 
    publish_date Date.parse("2011-05-02") 
    displayit true 

    after_create {|a| Factory(:category, :categoryable=>a)} 
    end 
end 


FactoryGirl.define do 
    factory :category do 
    sequence(:name) {|n| "category#{n}" } 
    end 
end 


FactoryGirl.define do 
    factory :categories_categoryables do 
    association :category 
    association :categoryable, :factory => :blog 
    end 
end 

但我似乎無法得到它的工作,有沒有人有任何想法我可以設置它?我不知道如果我在正確的地方創建後,我試着在categories_categoryables中調用它,但這似乎也沒有幫助。

謝謝

回答

1

當你做關聯時,你仍然需要爲你的類別定義工廠。例如:

FactoryGirl.define do 
    factory :categories_categoryables do 
    association :category, :factory => :category 
    association :categoryable, :factory => :blog 
    end 
end 

至少,這是我在工廠裏設置的方法。

+0

謝謝,但似乎沒有幫助。我認爲在博客之後我有一些問題,但我無法弄清楚我應該如何解決它 –