Using Rails to Create Meaningful Relations

Patrick Abejar
Sep 1, 2018 · 3 min read

Objects associate similarly related information about natural phenomena or other real-life nouns into an incredibly useful package. It is much more useful to have a customized artist object set as the attribute of a song rather than setting that same attribute to a string containing the artist’s name. It is much easier to set descriptive identifiable attributes such as hometown, genres, year started, and others to an artist object rather than a string representing an artist’s name. Recursively, we can provide attributes for hometown, genres, and years started, as long as we write our own classes for those attributes. One of the most interesting features of object-oriented programming lays in establishing relationships between any of these classes. This gives our data richer meaning beyond the scope of boring String or Fixnum attributes. We can build complex and powerful webs of relationships that will allow our genre object to provide us with all the song objects that belong to it, or all the song objects that an artist object has.

The Problem

Ideally, we want to be able to set an artist object equal to the artist attribute of a song object as shown below:

But almost none of our data is provided for us in the object format we desire. The Internet is filled with data flowing through forms as strings rather than a nice object. A user can’t simply type an object into an HTML form. You can only type in strings. We have to transform that user inputed string into an object. Only then can we take advantage of the previously mentioned powerful features objects offer us. In this example, it is the ability to create relationships among objects.

The Actual Case

A user types in the following information into a form that creates a song.

All this information is posted to a rails route that will now have control over all three of these user inputted strings. We can easily create objects using Rails.

But wait… Why do you want to create a differently named setter to store your user input text over the ActiveRecord produced method?

We like setting custom made objects as attributes, rather than strings and fixnums as these custom made objects can have different methods implemented for them.

Essentially, both methods work by setting an object as an attribute to another object. This is a shared goal. The difference lays in our input. One method takes in user text and another takes in an object. We have to that user text and transform it into an object.

When users type in information in a form, they often will not be typing an ID of an object, rather they will enter a descriptive name. This text should be used to either find or create an object. Once that object is instantiated and simultaneously persisted onto the database then we may form relations between it as follows:

  • If the object the form was intended for HAS_MANY of these newly created/found objects, then we must push the instance of this object onto the array.
  • If the object the form was intended for BELONGS_TO this newly created/found object, then we can set this instance of this object to an array.

Implementation

You will need to define custom getters and setters for the user text input you want to use to create or find an object. The setters will either push the object into an array (representing has_many relationship) or set the object equal to the attribute directly (representing a belongs_to relationship).

REMINDER: If this user field is left blank, we must not pass this into the create class method. We can either delete these empty strings or we can ignore them as we iterate through the params hash. This is performed in the model’s method.

Naming Conventions

The name of the getters and setters are dependent on the names provided in the HTML form. You will want to use a different name than the ActiveRecord defined setter to avoid self-referencing.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade