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

  • Home
  • SEARCH
  • 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 8024223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:48:15+00:00 2026-06-04T22:48:15+00:00

I have a :before_create method that performs some checks and returns false or true

  • 0

I have a :before_create method that performs some checks and returns false or true.

def create_subscription_on_gateway 
  gateway = self.keyword.shortcode.provider
  create_subscription = gateway.classify.constantize.new.create_subscription(self.phone,self.keyword_keyword,self.shortcode_shortcode,self.country)
  errors[:base] << "An error has occurred when finding gateway." if gateway.nil?
  errors[:base] << "No gateway found for this shortcode." if create_subscription.nil?
  errors[:base] << "Subscription could not be made." if create_subscription == false
end

Now, if the method returns false or nil I can see the errors on the form page okay. The problem is that the object has been saved to the database.

How can I prevent the object to be saved when there are still errors associated on it?

  • 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-04T22:48:16+00:00Added an answer on June 4, 2026 at 10:48 pm

    How about, instead of a before_create, you use validations. And then change your create_subscription_on_gateway to a before_validation

    validate :gateway_presence
    validate :gateway_found
    validate :create_subscription
    
    def gateway_presence
      if # ...your code here
        errors.add(:gateway, "An error has occured..."
      end
    end
    
    def gateway_found
      if # ...your code here
        errors.add(:gateway, "An error has occured..."
      end
    end 
    

    and so on…

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

Sidebar

Related Questions

I have a method that is called on an object to perform some business
I have a before_save defined as follows: def before_save self.token = generate_token end I
I have a before_save method that I call that renames an uploaded image. before_save
I have some plain text and html. I need to create a PHP method
I have a class hierarchy where __init__ in class Base performs some pre-initialization and
I have a method that converts a file to bytes so that I can
I want to create a method that compares a number but can have an
We have created a custom dataset and populating it with some data. Before adding
I have a before_filter in my Rails app that sends users to login_url if
I want to check my database for records that I already have recorded before

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.