title: Sinatra API Server Accessing Preexistent Database via Activerecord: about validations
body: <p>I'm experimenting great Sinatra environment to realize a json API server that access data from a "legacy" db, (an existent database, already in production).</p>
  
  <p>I'm enjoing sinatra-activerecord gem to access my data. as simple as include in the Sinatra app the simple decalration:</p>
  
  <pre><code>class Exam &lt; ActiveRecord::Base
  end
  </code></pre>
  
  <p>I'm enjoing too tux (the rails console equivalent), to query my models (tables).</p>
  
  <p>Now, I have an Architectural doubt about two different scenarios explained here below, in the case of inserting new records via an API call (let say a POST client request to Create/update a record):</p>
  
  <p><strong>Scenario 1. Existent DB as part of (my) Rails project, already in prod</strong></p>
  
  <p>suppose my production database (postgresql instance) is part of a Rails project developed by myself, so I have my Activerecord Models .rb code that contain quite complex validations methods; By example the above mentioned Model exam.rb consiste of a thousand of line of some complex regex validation methods...</p>
  
  <p>In that scenario, what is the best technical solution to avoid Model source code duplication (and so poossible mismatch errors) ? My I have to do in the Sinatra app, an OS symbolik link to the original Rails model exam.rb ... ?</p>
  
  <p><strong>Scenario 2. Existent DB developed by someone else</strong></p>
  
  <p>so suppose I have access at the database tables, but I do not have any source code about applicative data "validations" (maybe developed in Java or any code I do not access for some reasons...)</p>
  
  <p>In that scenario, I think the way is to re-write validations in ruby-activerecord; right ?</p>
  
  <p>sorry for my long post</p>
  
  <p>giorgio
  about.me/solyaris</p>