Using ActiveRecords Association to perfection.

Kelvin Lanier
2 min readJan 21, 2021

--

While completing a Sinatra MVC project where I used ActiveRecord association between two models, I realize how dynamic and versatile this ORM was. In Sinatra, an association is a connection between two Active Record models. Why do we need associations between models? Because they make common operations simpler and easier in your code.

For my Sinatra MVC I created a User model that has many routines. Each inherited various methods from ActiveRecords. Following, I will show you the methods for the belong to Routine object.

association

The association method returns the associated object, if any. If no associated object is found, it returns nil.

@user = @routine.user

association=(associate)

The association= method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associated object and setting this object's foreign key to the same value.

@routine.user = @user

build_association(attributes = {})

The build_association method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set, but the associated object will not yet be saved.

@user = @routine.build_user(user_number: 123,
user_name: "Kelvin Lanier")

The has_many association creates a one-to-many relationship with another model. In database terms, this association says that the other class will have a foreign key that refers to instances of this class. I have listed a few relavant methods for my has_many object:

collection

collection method returns a Relation of all of the associated objects. If there are no associated objects, it returns an empty Relation.

@routines = @user.routines

collection.build(attributes = {})

The collection.build method returns a single or array of new objects of the associated type. The object(s) will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will not yet be saved.

@routine = @user.routines.build(published_at: Time.now,
routine_number: "A7777")
@routines = @user.routines.build([
{ published_at: Time.now, routine_number: "A7778" },
{ published_at: Time.now, routine_number: "A7779" }
])

collection.create(attributes = {})

The collection.create method returns a single or array of new objects of the associated type. The object(s) will be instantiated from the passed attributes, the link through its foreign key will be created, and, once it passes all of the validations specified on the associated model, the associated object will be saved.

@routine = @author.routines.create(published_at: Time.now,
routine_number: "A23655")
@routines = @user.routines.create([
{ published_at: Time.now, book_number: "A12346" },
{ published_at: Time.now, book_number: "A12347" }
])

With this understanding of Active Record, persisting data becomes easier. We can now set up and work with models and build associations between them by leveraging methods such as has_many and belongs_to .

--

--

No responses yet