Gabe da Silveira
2013-Mar-07 11:11 UTC
Should ActiveRecord::RecordNotSaved attach the record in question?
I''m doing some work where I want to rescue ActiveRecord::RecordNotSaved from a save! call nested deep in the code. The thing is there could theoretically be failures here I want to pass through, and I want to inspect the record in order to decide. I figure a custom initializer on the exception class and a couple quick patches here and there in the code would make it minimally intrusive. I was thinking of whipping up a patch, but I wanted to ask here if there is some reason we definitely don''t want AR records attached to exceptions (memory leak risk or some other consider I''m not aware of). What are everyone''s thoughts on the feature? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Tejas Dinkar
2013-Mar-08 06:33 UTC
Re: Should ActiveRecord::RecordNotSaved attach the record in question?
On Mar-07-2013, at 4:41 PM, Gabe da Silveira <gabe@websaviour.com> wrote:> I''m doing some work where I want to rescue ActiveRecord::RecordNotSaved from a save! call nested deep in the code. The thing is there could theoretically be failures here I want to pass through, and I want to inspect the record in order to decide.I would usually not recommend using exceptions for program flow [1], especially for things like validation errors. Keeping this in mind, here is what I could think of: 1) save using the regular (non-!) method 2) inspect the errors on the object 3) throw an exception only if you are sure you want to abort your current action.> I was thinking of whipping up a patch, but I wanted to ask here if there is some reason we definitely don''t want AR records attached to exceptions (memory leak risk or some other consider I''m not aware of).The ActiveRecord::RecordInvalid exception already has a link to the model on which save was attempted. The object can be fetched via the .record method on the exception. If you are really keen on using exceptions, you could potentially catch the exception, inspect the errors as I described above, and then re-raise the exception if you want to abort. [1] http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl -- Tejas Dinkar http://c42.in C42 Engineering - The guys behind RubyMonk -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.