InicioLinuxActive Record : a useful tool
ActiveRecord is the package, inside Ruby on Rails developing framework, allowing the programmer to interact with a relational database.
The core of the logic of ActiveRecord is an object representing a record of a relational database. Such an object contains a Ruby-Hash, in which are stored all its fields, together with some additional hidden values: each hash key owns a set of specific methods to handle its corresponding field: the whole structure of an ActiveRecord object (i.e., an instance of a subclass of ActiveRecord::Base) is constructed around this field hash. Actually, ActiveRecord::Base is nothing more than a hash with rewritten methods, since it's possible to get manually the fields inside the class, by referring to them as self[:field].

An instance of ActiveRecord::Base is an object linked to a particular record, that can be modified in Ruby without affecting the database. Such an object can be handled as a normal element of an Object-Oriented programming language, and, through the methods “save” and “create”, it can be used to push modifications to its corresponding record in the database.
The typical advantage of ActiveRecords is the huge amount of "invisible" actions called automatically when we define a class. We can decide how to configure the methods of our model by just writing commands inside the body of the class. Doing this in the correct way, we produce a structure of objects in Ruby which allows us to read and write in the database very quickly. For example, we can create methods connecting our models, which lets us be able to find in a different table, with a single method call without parameters, all the records associated to ours.
This ability to configure dynamically the properties of our models is the essence of ActiveRecord's ease (the so called “ActiveRecord magic”). Moreover, it is available a big amount of shortcuts for the creation of methods, provided that you are willing to follow a standard nomenclature; anyway, if you prefere to do things manually, it is always possible to create a model in the explicit way.

Ruby, as a programming language, provides the great versatility necessary to implement dynamic method definitions of ActiveRecord. Commands as mentioned above, such as "belongs_to", "set_primary_key", or "validates_presence_of", even though they look like declarations, are actually methods themselves, defined in specific modules provided with the package. Each of them, when inserted inside the model and called, adds to its class the methods required, which do not limit to the couple of interface methods that we can use in the console (ActiveRecords in addition defines an amount of code under the visible surface of those methods, to keep order and links in the database).
And here comes the versatility of Ruby. The technic of those commands to work is through “singleton method definition”, that allows in Ruby to open the structure of an object and change it without touching its original class, only for that particular instance: ActiveRecord is allowed to add or rewrite in real time a method of a single instance of a class, according to which method constructors we put in its body. In some other cases, ActiveRecord just opens the general class and adds methods directly to it.
As an example of how this works, I can mention the object of class Array generated when we ask to ActiveRecords to retrieve us the list of records associated to ours through of a multiple association: even though its class results to be Array, this particular array has been constructed with some of its methods modified to work together with the other structures in the system.

To enter a bit more in detail with the characteristics of ActiveRecord, I can mention a small list of essential tools implemented in the system:
the definition of associations, allowing us to sail rapidly inside the database: these associations also define options to optimize the number of SQL-queries called from Ruby during the computation;
the definition of validations, which stop in advance any operation at risk to modify the database in undesired ways;
the definition of callbacks, to insert automatic behavior in any point of the cycle of extracting, validating, and saving a record.

I conclude with a few words to compare ActiveRecord to similar available technologies. Since I had barely had experiences with databases before, ActiveRecord has been my first real approach in this context: this means that I soon got used to handle database operations with ease, realizing only later how advantaged I was using ActiveRecord, when I saw the same operations performed by more common technologies, such as Java, PHP, or MySQL itself. ActiveRecord is just more rapid and easy to use and understand.

Autor: Adriano di Lauro

para ver esta nota y otras de nuestro blog hacé click aquí.
Datos archivados del Taringa! original
0puntos
0visitas
0comentarios
Actividad nueva en Posteamelo
0puntos
0visitas
0comentarios
Dar puntos:

Dejá tu comentario

0/2000

No hay comentarios nuevos todavía

Autor del Post

M
Maypun🇦🇷
Usuario
Puntos0
Posts13
Ver perfil →
PosteameloArchivo Histórico de Taringa! (2004-2017). Preservando la inteligencia colectiva de la internet hispanohablante.

CONTACTO

18 de Septiembre 455, Casilla 52

Chillán, Región de Ñuble, Chile

Solo correo postal

© 2026 Posteamelo.com. No afiliado con Taringa! ni sus sucesores.

Contenido preservado con fines históricos y culturales.