Question

I've been fighting with this all day. The problem is the same with Fabrication or FactoryGirl so I guess it has to do with Active Record or Rails.

Let's take the following code:

# app/models/category.rb
class Category < ActiveRecord::Base
  before_validation :set_order

  # Associations ------------------
  belongs_to :company

  # Validations -------------------
  validates :name, :order, :presence => true
  validates :order, :uniqueness => { :scope => :company, :message => "order should be unique" }

  def set_order
    return unless self.order.nil? || self.order.blank?

    company_categories = self.template && self.company.categories.template || self.company.categories.non_template
    self.order = (company_categories.empty? && 1) || (company_categories.map(&:order).sort.last + 1)
  end
end

# spec/factories/companies.rb
Fabricator :company do
  Fabricate.sequence(:name) { |n| "Company #{n}" }
  creation_date Date.today
  template false
end


# spec/factories/categories.rb
Fabricator :category do
  name ["Business", "Finance", "Analytics"][Random.rand(3)]
  template false
  company
end


# spec/models/category_spec.rb
require 'spec_helper'

describe Category do
  let(:category) { Fabricate(:category) }

  it "has a valid factory" do
    category.should be_valid
  end

end

Every time I run the spec, I get the following error:

1) Category has a valid factory
   Failure/Error: let(:category) { Fabricate(:category) }
   ActiveRecord::StatementInvalid:
     PG::Error: ERROR:  column categories.company does not exist
     LINE 1: ..."categories"  WHERE ("categories"."order" = 1 AND "categorie...
                                                                  ^
     : SELECT  1 AS one FROM "categories"  WHERE ("categories"."order" = 1 AND "categories"."company" IS NULL) LIMIT 1
   # ./spec/models/category_spec.rb:4:in `block (2 levels) in <top (required)>'
   # ./spec/models/category_spec.rb:7:in `block (2 levels) in <top (required)>'

It looks like the association is not set properly. To sql query is "categories"."company" and not "categories"."company_id". It is super weird.

Thanks for your help!

Was it helpful?

Solution

It is trying to run the validation on order. Your scope there is company which is where it is getting company instead of company_id from. Try:

validates :order, :uniqueness => { :scope => :company_id }

OTHER TIPS

db not ready yet. Run

rake db:migrate
rake db:test:prepare
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top