title: Rails Factories association fail
body: <p>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.</p>
  
  <p>Let's take the following code:</p>
  
  <pre class="lang-ruby prettyprint-override"><code># app/models/category.rb
  class Category &lt; ActiveRecord::Base
    before_validation :set_order
  
    # Associations ------------------
    belongs_to :company
  
    # Validations -------------------
    validates :name, :order, :presence =&gt; true
    validates :order, :uniqueness =&gt; { :scope =&gt; :company, :message =&gt; "order should be unique" }
  
    def set_order
      return unless self.order.nil? || self.order.blank?
  
      company_categories = self.template &amp;&amp; self.company.categories.template || self.company.categories.non_template
      self.order = (company_categories.empty? &amp;&amp; 1) || (company_categories.map(&amp;: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
  </code></pre>
  
  <p>Every time I run the spec, I get the following error:</p>
  
  <pre><code>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 &lt;top (required)&gt;'
     # ./spec/models/category_spec.rb:7:in `block (2 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>It looks like the association is not set properly. To sql query is "categories"."company" and not "categories"."company_id". It is super weird.</p>
  
  <p>Thanks for your help!</p>