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?
How about, instead of a before_create, you use validations. And then change your create_subscription_on_gateway to a before_validation
and so on…