v1.0.0 -
Show latest stable
-
3 notes
- Superclass:
ActiveRecord::ActiveRecordError
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (1)
- 2.1.0 (0)
- 2.2.1 (7)
- 2.3.8 (0)
- 3.0.0 (13)
- 3.0.9 (-8)
- 3.1.0 (1)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (38)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- 7.2.3 (0)
- 8.0.0 (0)
- 8.1.1 (0)
- What's this?
Raised by save! and create! when the record is invalid. Use the record method to retrieve the record which did not validate.
begin complex_operation_that_calls_save!_internally rescue ActiveRecord::RecordInvalid => invalid puts invalid.record.errors end
Register or
log in
to add new notes.
jqr -
February 14, 2009
noxyu3m -
February 14, 2009
schmidt -
April 15, 2009
1 thank
Clear and simple rescue
noxyu3m, your code is rescuing all exceptions, not just ActiveRecord::RecordInvalid.
I think this syntax is a bit more clear than using the global variable.
def create @model = Model.new(params[:model) @model.save! rescue => err # rescues all exceptions logger.error(err.to_s) end
0 thanks
Simple rescue
Take it easy:
def create @model = Model.new(params[:model) @model.save! rescue logger.error(!$.to_s) end
Global variable !$ refers to the Exception object.
0 thanks
Using global $! to address exception
@noxyu3m: Your code is actually syntactically wrong. The global is called $!
Your code should have been:
def create @model = Model.new(params[:model) @model.save! rescue logger.error($!.to_s) end
Although I would prefer
def create @model = Model.new(params[:model) @model.save! rescue ActiveRecord::RecordInvalid logger.error($!.to_s) end
to only catch expected exceptions, just like the documentation proposed.

