ruby on rails - Refactoring validation methods and callbacks -


i using ruby on rails 3.0.7 , have tree classes behavior same (and code in them model files). have name , description attribute, run same validation methods , both there before_save callback maintains data consistent providing same functions.

i refactor validation methods , callbacks in separated class\model (i think have locate them related files in \lib folder of application).

what have make that? code have add in classes , in refactoring class\model?

well, make super class 3 models inherit. tend put abstract base class in app/models alongside models themselves.

# app/models/thing.rb class thing < activerecord::base     # common code goes here, such     before_save ...     validates_length_of :foo end  # app/models/red_thing.rb class redthing < thing     # methods specific redthing go here end  # app/models/blue_thing.rb class bluething < thing     # methods specific bluething go here end 

if things have many differences such doesn't make sense group them this, you'd want use module instead, bit more complicated.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -