Database with Rails
May 4th, 2008
Getting Started
Database access is done through the ActiveRecord object.
Make sure you know what a virtual attribute is. I recommend you view all the active record screencasts from railscast.com.
You setup and manipulate your database using migrations. A helpful Rails Migration Cheetsheet can be found at dizzy.co.uk
Table Associations
In Rails, databases are tied together behind the scenes by ActiveRecord. By adding an appropriate directive in the model class you inform ActiveRecord how to handle these relationships. Examples of these directives include: has_many, has_one, belongs_to, has_many :through, has_many_polymorphs
1 2 3 |
class Person < ActiveRecord::Base has_many_polymorphs :ownables, :from => [:dvds, :cars, :books], :through => :ownerships end |
One of the best places to find articles on associations that I have found is at blog.hasmanythrough.com
IS A - What's polymorph
Recently I strugged to work out how to represent an IS A relationship between my models.
See my notes IS A - What's a polymorph for my first attempt at solving it and some links to good articles on polymorph.
Leave a Reply