Saturday, August 29, 2009

Rails Basics : What is a Model ?

What is a Model ?

The Model is the 'M' in the MVC (Model/Views/Controller) concept integral to a Rails application.

Models should :-
  • Constitute most of your application codes. 
  • Provide  a persistent storage mechanism to your database
  • Define all business logic
In a nutshell, Models should be the brains that operate on your application's database, determine and change the state of other objects and co-ordinate the overall business logic of your applications.

Conventions for Models

Model classes in Rails are all inherited from the ActiveRecord base class ActiveRecord::Base and map one-to-one to a table in your database.

Residing in the app/models directory, Models should following the following convention :-
  • Only one class per file
  • Class name should be singular and camel-cased eg.Customer or BillNo
  • Corresponding table name should be lowercased, plural and underscored eg. customers or bill_nos
  • Corresponding table must have an auto-incrementing integer field called id
  • Column names shoudl also be lowercased
  • Model filename should be lowercased and underscored version of the class name eg. customer.rb or bill_no.rb
To represent the relationship between tables and columns in your application, ActiveRecord provides the following set of methods called associations :-
  • has_many represents a zero-to-many relationship between the Parent & Child Class
  • belongs_to is the reciprocal child method to parent has_many method
  • has_one  is like a belongs_to and represents a one-to-one relationship in your database


More to come...

No comments:

Welcome to Rails.. Rails... Rails !

In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.

As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam

CCH
10th June 2010, 19:42