ruby on rails - How to save something to the database after failed ActiveRecord validations? -


basically want log action on mymodel in table of mymodellog. here's pseudo code:

class mymodel < activerecord::base   validate :something    def      # test      errors.add(:data, "bug!!")   end end 

i have model looking this:

class mymodellog < activerecord::base    def self.log_something     self.create(:log => "something happened")   end  end 

in order log tried :

  • add mymodellog.log_something in something method of mymodel

  • call mymodellog.log_something on after_validation callback of mymodel

in both cases creation rolled when validation fails because it's in validation transaction. of course want log when validations fail. don't want log in file or somewhere else database because need relationships of log entries other models , ability requests.

what options?

nested transactions seem work in mysql.

here tried on freshly generated rails (with mysql) project:

./script/generate model event title:string --skip-timestamps --skip-fixture  ./script/generate model eventlog error_message:text --skip-fixture  class event < activerecord::base                                                                                                                                          validates_presence_of :title                                                                                                                                            after_validation_on_create :log_errors                                                                                                                                   def log_errors                                                                                                                                                            eventlog.log_error(self) if errors.on(:title).present?                                                                                                                end                                                                                                                                                                   end    class eventlog < activerecord::base                                                                                                                                       def self.log_error(event)                                                                                                                                                 connection.execute('begin') # if transaction doesn't work.     create :error_message => event.errors.on(:title)                                                                                                 connection.execute('commit')                                                                                                                                          end                                                                                                                                                                   end   # , in script/console: >> event.new.save => false >> eventlog.all => [#<eventlog id: 1, error_message: "can't blank", created_at: "2010-10-22 13:17:41", updated_at: "2010-10-22 13:17:41">] >> event.all => [] 

maybe have on simplified it, or missing point.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -