多对多多态模型-设置工厂女孩



我环顾四周,建立了一个多态的多对多模型,似乎是有效的。我这样设置:

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

但是我似乎不能让它工作,有人知道我该如何设置它吗?我不确定我是否有后创建在正确的地方,我试过调用它在categores_categoryables,但这似乎没有帮助。

谢谢

在进行关联时仍然需要为类别定义工厂。比如:

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

至少,在我的工厂里是这样设置的。

最新更新