0
  1. Can somebody please explain me the difference between model and active record in simple words ?

  2. Is active model and model same ?

PS: Knowledge regarding these topics will be appreciated.

I am newbie and started learning on my own, but I got stuck in these basics. Please provide explanation of any other term related to model or active record.

3
  • a model that you define in your application inherits from ActiveRecord::Base. So by virtue of Ruby's inheritance scheme, your model has all the methods that it inherits from ActiveRecord::Base. This is why you can find an instance of your model from the database by MyModel.find(567) because ActiveRecord::Base provides the find class method for you. Commented Jul 9 at 14:38
  • RoR is an MVC (Model-View-Controller) framework. Generally when someone refers to a "Model" in a rails application they are referring to a database backed object that inherits directly or indirectly from ActiveRecord::Base. You have also identified that ActiveModel exists. This class provides a lot of the basic object functionality for ActiveRecord, sans the database methods. Rails is a very well documented library so for instance ActiveRecord and ActiveModel Commented Jul 9 at 14:55
  • ActiveRecord comes with Rails. Your Models use ActiveRecord to interact with the database. Commented Jul 9 at 21:05

2 Answers 2

1

Can somebody please explain me the difference between model and active record in simple words ?

I'll try but these are pretty high level concepts.

Models

Models are a wrapper around the data and the related logic (methods) in the Model View Controller Pattern (MVC) which Rails is a flavor of.

A model is a class that encapsulates the buisness logic in your application. You can think of them as the representation of the "things" your app deals with such as users, products, articles, etc. You can think of this in contrast to the mess you would get if your application just passed around "raw" data in the form of hashes and arrays.

The data in a model doesn't have to correspond to records in the database. So you can say that ActiveRecord Models are Models but not all Models are ActiveRecord. A model can for example wrap data from an API or use other backends to store that data which are not even a relational database.

What is particular to models in Rails flavor MVC is how chunky they are. Rails uses relatively few concepts and throws a lot of logic into the model layer.

Rails provides the building blocks to make any class act like a model through the ActiveModel::Model API.

ActiveRecord

ActiveRecord is an Object Relational Manager (ORM) that's included in Rails by default. It is a separate gem and can be used outside of Rails but the development of the two are integrally linked. It's loosly inspired by Martin Fowler's 2003 book Patterns of Enterprise Application Architecture.

You can think of it kind of like the translator between your application and the database which speak completely different languages:

User.find(1)

Turns into this SQL:

SELECT * FROM "users" WHERE "users"."id" = ?

The application is object oriented while a database is centered arount tables divided into rows and columns. ActiveRecord lets your models both read data from and save data to the database.

ActiveRecord works with multiple relation database management systems such as MySQL/MariaBD, Oracle, SQLite and Postgres and to a degree abstracts away the differences.

What is significant about ActiveRecord compared to other ORM's is how little configuration is needed and how heavily it leans into metaprogramming. In other ORMs you intially need to tell it everything about the database table and what attributes the model should have.

ActiveRecord instead leans heavily into the ideas of Convention over Configuration and assumes there is a relationship between the names of your classes and your database tables and "magically" modifies the class by reading the table defintions from the database.

ActiveRecord makes models even more chunky as the same class fills the role of a repository (the class that you use when querying) and the class that represents the data from the table.

0

In Ruby on Rails (Rails), a model is part of the MVC (Model-View-Controller) architecture. It represents your application's data as well as its business logic. In other words, a model correlates to a table in your database, while instances of the model correspond to rows in that table.

Active Record is a design pattern for object-relational mapping (ORM). Active Record is Rails' default ORM and a library. It allows you to communicate with the database through Ruby code rather than SQL queries. When you define a model in Rails, it will immediately inherit from ActiveRecord::Base, which has a number of methods for creating, reading, updating, and deleting records in the database.

Active Model: It's a module that includes various functionalities to make objects behave like an Active Record object without requiring a database table. This is useful for things like form objects, service objects, or any other Ruby object that needs to interact with Rails features (like validations, callbacks, or serialization) but doesn't need to be a full Active Record model.

In other words, Active Model gives you a way to create objects that have some of the same capabilities as Active Record models but don't necessarily map to a database table.

Model: In Rails, when we refer to a model, we usually mean a class that inherits from ActiveRecord::Base and is backed by a database table.

Not the answer you're looking for? Browse other questions tagged or ask your own question.