2

I'm currently trying to implement Multi-table inheritance in Factory Girl. So I have a model "business" with parameter "biz_type" and there are different types, models of businesses like bar, restaurant, hotel and so on. What I am trying to do is to create an association "biz" which would link to one of those models of biz_types. As you see, the biz models would be generated sequentially. So the first would be bar, then restaurant, hotel and again bar.. so on.

However it seems I can't access the value of the "biz_type" sequence. When I run this code the error in block (2 levels) in <top (required)>': undefined method downcase' for #<FactoryGirl::Declaration::Implicit:0x000000034c6550> (NoMethodError) occurs. I've already tried to access the value through biz_type.to_attributes[0].to_proc.call.downcase.to_sym. That was almost successful, although it's kind of hacky solution and also it always gives just the first type of biz, in our example "bar".

Is there any nice solution how to set up business factory so that each business.biz would give different generated biz model?

FactoryGirl.define do
  factory :business do    
    sequence :biz_type do |n|      
      case n % 3
      when 1
        "Bar"
      when 2
        "Restaurant"
      when 0
        "Hotel"
      end      
    end    
    association :biz, factory: biz_type.downcase.to_sym    
  end
end

in block (2 levels) in <top (required)>': undefined method downcase' for #<FactoryGirl::Declaration::Implicit:0x000000034c6550> (NoMethodError)

4

0 回答 0