ActiveRecord – first impressions
I finally had enough ideas, material and time to actually write some code to my project. So far I have mostly played with ActiveRecord, and what a wonderful experience it has been!
I liked:
- Declarative programming of relationships and validations
- The power of
has_many :through - Model inheritance "Rails way" aka single table inheritance or
:polymorphic => true
I particularly enjoyed playing with the model classes using Rails console seeing the "magic" (SQL) happening. Single table inheritance (STI) seemed a bit silly idea at first. In practice STI works extremely well, if your inherited models do not have additional attributes, but might e.g. have additional validation rules.
As always, not everything was just smooth as silk, and a few times ActiveRecord did not work the way I wanted.
I did not like:
- Model attributes are not visible in the Ruby code of model (I need to check out the schema.rb, migration files or database).
- If I define a database level constraint "no null", ActiveRecord will not validate the related attribute. Instead it will silently store an empty string.
These bugs I found:
- ActiveRecord does not support Ruby namespaces. For instance, STI will not use fully qualified class name in
typefield (e.g.Kennelinstead ofStakeholders::Kennel). :polymorphic => trueand STI together will use incorrectmodel_typefield (e.g. "abstact"Animalinstead of STI inheritedDog).
Despite a few tiny issues, ActiveRecord seems a great framework for persisting your models with a relational database.
