0

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
end

I 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"
end

Running the test I get:

Failure/Error: product = FactoryGirl.create(:product)
 NoMethodError:
  undefined method `productsubclass=' for #<Product:0xcd42090>
user929062
  • 807
  • 4
  • 12
  • 33
  • That's weird, productsubcategory is not mentioned in the models - have you simplyfied them when posting the question? – Neil Billingham Feb 02 '14 at 13:38
  • Sorry Neil, I just mad a copy mistake... it is productsubclass in the error message. I have corrected it above... – user929062 Feb 02 '14 at 14:11
  • possible duplicate of [How to set up factory in FactoryGirl with has\_many association](http://stackoverflow.com/questions/6963298/how-to-set-up-factory-in-factorygirl-with-has-many-association) – Marcelo De Polli Feb 02 '14 at 16:50
  • I have seen the other question, but unfortunately it didn't help me out... – user929062 Feb 02 '14 at 17:06

1 Answers1

1

I 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
Neil Billingham
  • 2,235
  • 4
  • 23
  • 34
  • This is tough. I couldn't get it working with the callbacks and - to be honest - I didn't fully understand the logic behind it. I even tried something like this in my spec to avoid the hassle in the factories.rb: @productmainclass = FactoryGirl.create(:productmainclass) (ok!), then @productsubclass = FactoryGirl.create(:productsubclass, productmainclass: @productmainclass) (ok!) and finally product = FactoryGirl.create(:product, productsubclass: @productsubclass, productmainclass: @productmainclass), but here it fails with undefined method `productsubclass=' for # – user929062 Feb 02 '14 at 20:36