title: Which rails ActiveRecord callback to sync with service (stripe) when creating a new record and still properly use errors?
body: <p>I have a User and a StripeCustomer model.   Every User embeds one and accepts_nested_attributes_for StripeCustomer.</p>
  
  <p>When creating a new user, I always create a corresponding StripeCustomer and if you provide either a CC or a coupon code, I create a subscription.</p>
  
  <p>In my StripeCustomer:</p>
  
  <pre><code>attr_accessible :coupon_id, :stripe_card_token
  </code></pre>
  
  <p>What I'd like to do is, if the coupon is invalid, do:</p>
  
  <pre><code>errors.add :coupon_id, "bad coupon id"
  </code></pre>
  
  <p>So that normal rails controller patters like:</p>
  
  <pre><code>if @stripe_customer.save
      ....
  else
      ....
  end
  </code></pre>
  
  <p>will just work.  And be able to use normal rails field_with_errors stuff for handling a bad coupon.</p>
  
  <p>So the question is, at which active record callback should I call Stripe::Customer.create and save the stripe_customer_token?</p>
  
  <p>I had it on before_create, because I want it done only if you are really going to persist the record.   But this does strange things with valid? and worse, if you are going to create it via a User, the save of User and StripeCustomer  actually <em>succeeds</em> even if you do errors.add in the before_create callback!   I think the issue is that the save will only fail if you add errors and return false at before_validation.</p>
  
  <p>That last part I'm not sure if it is a mongoid issue or not.</p>
  
  <p>I could move it to before_validation :on => :create but then it would create a new Stripe::Customer even if I just called valid? which I don't want.</p>
  
  <p>Anyway, I'm generically curious about what the best practices are with any model that is backed by or linked to a record on a remote service and how to handle errors.</p>