title: Factory Girl with legacy read only database
body: <p>I am setting up tests for an application that links to a read only legacy database.</p>
  
  <p>I have 2 models ProductSpecification and Stock. The Stock model is accessing read only data on a legacy database.</p>
  
  <pre><code>class ProductSpecification &lt; ActiveRecord::Base
    belongs_to :stock
    validates :stock_id, uniqueness: true, presence: true
  
    def stock_name
      stock.name
    end
  end
  
  class Stock &lt; ActiveRecord::Base
    def readonly?
      @readonly = true
    end
  end
  </code></pre>
  
  <p>I need to set up the factory with a stock name but adding a stock_name writer method in ProductSpecification violates the fact that Stock is readonly. </p>
  
  <p>To get around this I have monkey patched the ProductSpecification in the factory file.</p>
  
  <pre><code>class ProductSpecification &lt; ActiveRecord::Base
    def stock_name= name
      # the stock table is read only to a legacy database
      # this monkey patch allows a stock name to be set without adding stock
    end
  end
  
  FactoryGirl.define do
    factory :product_specification do
      sequence(:stock_id)
      sequence(:stock_name){ |n| "Stock Name #{n}" }
    end
  end
  </code></pre>
  
  <p>This allows me to set a stock_name purely for the tests.</p>
  
  <p>Is there a better practice to follow here?
  Is there an alternative solution to the monkey patch I haven't considered?</p>