Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8192775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:15:06+00:00 2026-06-07T04:15:06+00:00

I have a User and a StripeCustomer model. Every User embeds one and accepts_nested_attributes_for

  • 0

I have a User and a StripeCustomer model. Every User embeds one and accepts_nested_attributes_for StripeCustomer.

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.

In my StripeCustomer:

attr_accessible :coupon_id, :stripe_card_token

What I’d like to do is, if the coupon is invalid, do:

errors.add :coupon_id, "bad coupon id"

So that normal rails controller patters like:

if @stripe_customer.save
    ....
else
    ....
end

will just work. And be able to use normal rails field_with_errors stuff for handling a bad coupon.

So the question is, at which active record callback should I call Stripe::Customer.create and save the stripe_customer_token?

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 succeeds 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.

That last part I’m not sure if it is a mongoid issue or not.

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:15:07+00:00Added an answer on June 7, 2026 at 4:15 am

    Ok here is what I did, I split the calls to stripe into 2 callbacks, one at before_validation and one before_create (or before_update).

    In the before_validation, I do whatever I can to check the uncontrolled inputs (directly from user) are valid. In the stripe case that just means the coupon code so I check with stripe that it is valid and add errors to :coupon_code as needed.

    Actually creating/updating customers with stripe, I wait to do until before_create/before_update (I use two instead of just doing before_save because I handle these two cases differently). If there is an error then, I just don’t handle the exception instead of trying to add to errors after validation which (a) doesn’t really make any sense and (b) sort of works but fails to prevent saves on nested models (in mongoid anyway, which is very bad and strange).

    This way I know by the time I get to persisting, that all the attributes are sound. Something could of course still fail but I’ve minimized my risk substantially. Now I can also do things like call valid? without worrying about creating records with stripe I didn’t want.

    In retrospect this seems pretty obvious.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have User and Friend entities in my data model, being a one User
I have a plugin with user model, profile model and an user controller, in
I have user profile model with M2M field class Account(models.Model): ... friends = models.ManyToManyField('self',
I have user details files which I get every month end. File will have
I have User model and I have following in the User model def name
Suppose I have two entities: User and UserGroup . They have a one-to-many relationship
I have User model with static method authenticate and I use Rubyoverflow gem. In
I have User model. I have Comment model. Here is their relation: User.rb: has_many
Let's say I have User model and Post model. Post model contains field user_id
I have a user model (sql table) that has_one : profile. profiles have user_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.