Ruby on Rails
UnderstandingModels

Models (see UnderstandingMVC for an overview) are the heart of a system. They model the important objects in the system, like users, products, prices, preferences, and so on.

In general, a computer system’s model should be independent of how people interact with it and how its data should be stored. In Rails, models are typically based on ActiveRecord, which means they are not separated from their storage, but that’s a pragmatic decision based on making the simple cases really easy.

ActiveRecord is a simple case of ORM (object-relation mapping) whereby your database comes alive, one row of a table at a time. It also handles relationships between objects (one-one, one-many, many-many, composition) and validation.

Using ActiveRecord, you’re placing (most of) your model close to the database. If you need greater abstraction, you can use ActiveRecord judiciously or not at all. Many people have written Rails applications with file- or RAM-based backends. There are other ORMs for Ruby, and you could use any of them with Rails if you please.

The only important thing about your model in a Rails app is that it gives the right answer when the controller asks it a question.

See:

category: Understanding