ruby - Why don't I see more constructors in rails models -


it seems rarely, if ever, see or use ruby constructor in rails model.

my assumption because rails sets stuff need initialization code lower.

aren't there use cases constructor in model though?

there's nothing wrong constructor, they're hardly ever needed. main reason constructor setting default values. setting default attributes, done @ database level

add_column :users, :admin, :boolean, :default => false 

the main other thing might needed existence/creation of association model, can done either in constructor/initializer, what's more common use rails hooks set before_create :populate_children, :ensure_parent_exists (where populate_children, , ensure_parent_exists private model methods) or that. approach means initialization logic can divided logical methods (e.g. separate methods each bit of initialization), , can additionally called @ other times after_save :ensure_parent_exists, , allows more flexibility


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -