我有以下型号:
class Productmainclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings has_many :productsubclasses end class Productsubclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings belongs_to :productmainclass end class Product < ActiveRecord::Base attr_accessible :name, :productimage, :size, :description, :price has_many :producttaggings, :dependent => :destroy has_many :productsubclasses, :through => :meteoritetaggings has_many :productmainclasses, :through => :meteoritetaggings mount_uploader :productimage, ProductimageUploader end class Producttagging < ActiveRecord::Base belongs_to :product belongs_to :productsubclass belongs_to :productmainclass attr_accessible :product_id, :productsubclass_id, :productmainclass_id end我现在想用FactoryGirl和Capybara创建一个产品。 在规范中我只是:
product = FactoryGirl.create(:product)在我的工厂里。我有:
factory :product do name "Blue glass" description "Description text of product" productimage File.new(File.join(::Rails.root.to_s, "spec/factories/", "testimage.jpg"), 'rb') productsubclass productmainclass end factory :productsubclass do name "Colored glasses" productmainclass end factory :productmainclass do name "Glasses" end运行测试我得到:
Failure/Error: product = FactoryGirl.create(:product) NoMethodError: undefined method `productsubclass=' for #<Product:0xcd42090>I have the following models:
class Productmainclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings has_many :productsubclasses end class Productsubclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings belongs_to :productmainclass end class Product < ActiveRecord::Base attr_accessible :name, :productimage, :size, :description, :price has_many :producttaggings, :dependent => :destroy has_many :productsubclasses, :through => :meteoritetaggings has_many :productmainclasses, :through => :meteoritetaggings mount_uploader :productimage, ProductimageUploader end class Producttagging < ActiveRecord::Base belongs_to :product belongs_to :productsubclass belongs_to :productmainclass attr_accessible :product_id, :productsubclass_id, :productmainclass_id endI now want to create a Product with FactoryGirl and Capybara. In the spec I simply have:
product = FactoryGirl.create(:product)In my factories.rb I have:
factory :product do name "Blue glass" description "Description text of product" productimage File.new(File.join(::Rails.root.to_s, "spec/factories/", "testimage.jpg"), 'rb') productsubclass productmainclass end factory :productsubclass do name "Colored glasses" productmainclass end factory :productmainclass do name "Glasses" endRunning the test I get:
Failure/Error: product = FactoryGirl.create(:product) NoMethodError: undefined method `productsubclass=' for #<Product:0xcd42090>最满意答案
我认为你设置它的方式如果处理的情况是:产品属于productsubclass,那么产品和productsubclass将使用product.productsubclass_id很好地插入并且一切都很好,但这显然不是你的结构,所以我们必须用另一种方式。 我认为@depa指出的链接是正确的方法,特别是本文档中的“基本有很多关联”部分: http ://robots.thoughtbot.com/aint-no-calla-back-girl虽然你有增加了has_many的复杂性。 但基本上你在看你创建一个对象的情况,然后你触发另一个创建来制作一个对象。 希望这有道理:)
**更新**
这是另一种可能有限的方法,但您可以从另一个方向创建记录。 那么,如果你只想在每个对象/表中有一条记录,那么:
FactoryGirl.define do factory :producttagging do product productsubclass productmainclass end endI think the way you have it setup would work if you were dealing with a situation where :product belonged to productsubclass, then the product and the productsubclass would be created with the product.productsubclass_id nicely inserted and all would be fine, but that's clearly not your structure, so we'd have to use another way. I think the link that @depa noted is the right way to go, specifically the 'Basic has many associations' section in this document: http://robots.thoughtbot.com/aint-no-calla-back-girl although you have the added complexity of a has_many through. But essentially your looking at a situation where you create an object and then after that you trigger another create to make the many's. Hope this makes sense :)
** Update **
Here's another approach which might be a little limited but you could just create the records from the other direction. So, if you just want one record in each object/table how about this:
FactoryGirl.define do factory :producttagging do product productsubclass productmainclass end endFactoryGirl with has_many:通过与水豚的联系(FactoryGirl with has_many :through associations with capybara)我有以下型号:
class Productmainclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings has_many :productsubclasses end class Productsubclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings belongs_to :productmainclass end class Product < ActiveRecord::Base attr_accessible :name, :productimage, :size, :description, :price has_many :producttaggings, :dependent => :destroy has_many :productsubclasses, :through => :meteoritetaggings has_many :productmainclasses, :through => :meteoritetaggings mount_uploader :productimage, ProductimageUploader end class Producttagging < ActiveRecord::Base belongs_to :product belongs_to :productsubclass belongs_to :productmainclass attr_accessible :product_id, :productsubclass_id, :productmainclass_id end我现在想用FactoryGirl和Capybara创建一个产品。 在规范中我只是:
product = FactoryGirl.create(:product)在我的工厂里。我有:
factory :product do name "Blue glass" description "Description text of product" productimage File.new(File.join(::Rails.root.to_s, "spec/factories/", "testimage.jpg"), 'rb') productsubclass productmainclass end factory :productsubclass do name "Colored glasses" productmainclass end factory :productmainclass do name "Glasses" end运行测试我得到:
Failure/Error: product = FactoryGirl.create(:product) NoMethodError: undefined method `productsubclass=' for #<Product:0xcd42090>I have the following models:
class Productmainclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings has_many :productsubclasses end class Productsubclass < ActiveRecord::Base attr_accessible :name, :id, :maintext has_many :producttaggings, :dependent => :destroy has_many :products, :through => :producttaggings belongs_to :productmainclass end class Product < ActiveRecord::Base attr_accessible :name, :productimage, :size, :description, :price has_many :producttaggings, :dependent => :destroy has_many :productsubclasses, :through => :meteoritetaggings has_many :productmainclasses, :through => :meteoritetaggings mount_uploader :productimage, ProductimageUploader end class Producttagging < ActiveRecord::Base belongs_to :product belongs_to :productsubclass belongs_to :productmainclass attr_accessible :product_id, :productsubclass_id, :productmainclass_id endI now want to create a Product with FactoryGirl and Capybara. In the spec I simply have:
product = FactoryGirl.create(:product)In my factories.rb I have:
factory :product do name "Blue glass" description "Description text of product" productimage File.new(File.join(::Rails.root.to_s, "spec/factories/", "testimage.jpg"), 'rb') productsubclass productmainclass end factory :productsubclass do name "Colored glasses" productmainclass end factory :productmainclass do name "Glasses" endRunning the test I get:
Failure/Error: product = FactoryGirl.create(:product) NoMethodError: undefined method `productsubclass=' for #<Product:0xcd42090>最满意答案
我认为你设置它的方式如果处理的情况是:产品属于productsubclass,那么产品和productsubclass将使用product.productsubclass_id很好地插入并且一切都很好,但这显然不是你的结构,所以我们必须用另一种方式。 我认为@depa指出的链接是正确的方法,特别是本文档中的“基本有很多关联”部分: http ://robots.thoughtbot.com/aint-no-calla-back-girl虽然你有增加了has_many的复杂性。 但基本上你在看你创建一个对象的情况,然后你触发另一个创建来制作一个对象。 希望这有道理:)
**更新**
这是另一种可能有限的方法,但您可以从另一个方向创建记录。 那么,如果你只想在每个对象/表中有一条记录,那么:
FactoryGirl.define do factory :producttagging do product productsubclass productmainclass end endI think the way you have it setup would work if you were dealing with a situation where :product belonged to productsubclass, then the product and the productsubclass would be created with the product.productsubclass_id nicely inserted and all would be fine, but that's clearly not your structure, so we'd have to use another way. I think the link that @depa noted is the right way to go, specifically the 'Basic has many associations' section in this document: http://robots.thoughtbot.com/aint-no-calla-back-girl although you have the added complexity of a has_many through. But essentially your looking at a situation where you create an object and then after that you trigger another create to make the many's. Hope this makes sense :)
** Update **
Here's another approach which might be a little limited but you could just create the records from the other direction. So, if you just want one record in each object/table how about this:
FactoryGirl.define do factory :producttagging do product productsubclass productmainclass end end
发布评论