DEV Community

kleach12
kleach12

Posted on

Relationship Goals

Learning Ruby and how to communicate with the databases can feel like a handful. Not only did you have to learn a whole language and its concept but you also needed to learn SQL and its queries to interact with a database. This can feel overwhelming at first as it is a lot of information that is required but luckily we can thank Active Records for making our lives easier. Active records give us the ability to easily interact and perform queries on these databases. Instead of having to write out a several-sentence query, it's as easy as saying create this or destroy that. Not only does it allow us to perform queries but it also gives us the ability to create associations between tables. These associations allow for our tables to be linked together whether it is by a one-to-many relationship or a many-to-many relationship. These relationships give us the ability to link said tables and perform methods and gather information from one table to another and are an extremely important concept to understand for a backend developer.

A one-to-many relationship is pretty straightforward and means one thing has several connections. An example of this would be a user on Twitter who has several tweets or another real-life example would be one person who has several groceries. There are an endless amount of real-world examples of a one-to-many relationships. In my project, I decided to create a job tracking application. This application has several jobs and each of these jobs has a type. The one-to-many relationship would be that one type i.e. MANGA could have several jobs linked to it.

class Category < ActiveRecord::Base
  has_many :jobs
end

class Job < ActiveRecord::Base
  belongs_to :category 
end
Enter fullscreen mode Exit fullscreen mode

Now, this, for instance, would not be the only one-to-many relationship and many more could be added. For example, I can create more one-to-many relationships if I were to expand the application and allow for it to have several users who could sign in and track their jobs. This would be a better example to show the importance of one-to-many relationships and active record associations. It is important that when a user logs in they are only able to see their jobs, as there would be no reason for them to see others’ jobs. So when a user is created it is important that the user is given a unique ID and that ID from then on is linked to every job they create. There are several methods that can be created to interact with a one-to-many relationship.

A many-to-many relationship is two databases that are linked to a third database. For example, if there is an application that rates books and each book has reviews and for that review, there is a user. There we can see that there are two one-to-many relationships. The first is that each book could have several reviews and each user could write several reviews. But what would the relationship between the users and the book be? That is your many to many relationships, books can have several users and users can have several books. In my project, there was no opportunity for a many-to-many relationship as I did not expand that far but I do believe there is a possibility for one. To implement a many-to-many relationship we would look at the users who are creating the jobs and the types of jobs they are creating. For every user, there are several job types that they can choose from and vice versa for job types and users.

class User < ActiveRecord::Base
  has_many :jobs

end

class User < ActiveRecord::Base
  has_many :jobs

end

class Job < ActiveRecord::Base
  belongs_to :user 
  belongs_to :category 

end
Enter fullscreen mode Exit fullscreen mode

These relationships are not the only in relational databases but they are the most common. Once you grasp the concept of relational databases and how they can interact with each other it makes working with your database easier.

Top comments (0)