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.
MyModel.find(567)
because ActiveRecord::Base provides thefind
class method for you.ActiveRecord::Base
. You have also identified thatActiveModel
exists. This class provides a lot of the basic object functionality forActiveRecord
, sans the database methods. Rails is a very well documented library so for instance ActiveRecord and ActiveModel