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
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
- 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:
Post a Comment