Donnerstag, 10. Juli 2008

Modeling Rails

Modeling Rails.

Basically, objects can connect to other objects in 3 different ways.
1. By relationship (i.e. person has one favorite book # person belongs_to favorite_book)
2. By inheritance (i.e. author is a person # class Author < Person
3. By Aggregation (i.e Invoice contains a list of items(objects) that have been aggrigated from other objects. composed_of :item :model => "Book"

When writing in rails model the application first.

1. map out the objects & their attributes for the application (i.e)
Customers : first_name, last_name, address
Books : author, title, price, EAN number
Invoice : customer, invoice_number, book

2. then map out the model connections
Customers (1---------n) Invoices
Customer (1---------1) Favorite_book
Invoice (1---------.) Books (invoice aggregates and copies the attributes of a book when added to an order).

3. Then think about how the objects and relationships can be mapped to the database (through active record).
i.e. which tables have which columns and how the models relate to each other - belongs_to, has_many, has_many_and_belongs_to, composed_of

Keine Kommentare: