DEV Community

Theodore Hoover
Theodore Hoover

Posted on

Active Record Association Methods and Possible Uses

When we use Active Record to build our projects we gain access to a lot of great functionality. One of the things Active Record gives us are the has_many and belongs_to associations, this post will go over some of the methods Active Record provides with those associatons.

has_many

The has_many association indicates that one object has many of another that belong to it. For instance in my last project, Strong Grips, each user had many hangs.

class User < ActiveRecord::Base
    has_many :hangs
    …
end
Enter fullscreen mode Exit fullscreen mode

One Active Record has_many method I used in that project is the .collection method. That gives us an array of all the objects indicated by the has_many statement that belong to the object the method is called on. In this case user.hangs was used to give an array of all hangs belonging to that user, that array was then used to display those hangs.

A has_many method I didn’t use but could have is .collection.build This method makes a new object and automatically sets the foreign key to indicate the new object belongs to the object the method was called on. The method can also be passed information to assign to the object, for instance: user.hangs.build(exercise: “deadhang”, duration: 100) This method can make multiple objects at once if passed an array. If I were to come back to this project I would use this method to make hangs on the users account from a form.

Another, similar method to the last one is .collection.create This also makes a new object and automatically sets the foreign key, but it also saves the object. The syntax is also very similar, an example call could be user.hangs.create(exercise: “deadhang”, duration: 100) Again, multiple objects can be made at once if the method is passed an array. This method would have been nice to use, again when making a hang on the users account, but I would need to restructure the way I verify that hangs are valid.
If this method is to be used then good validations need to be made on the object it is creating, because it is saved right after checking against those validations.

belongs_to

The belongs_to association indicates that each instance of that object belongs to an instance of another object. In Strong Grips each hang belonged to a user.

class Hang < ActiveRecord::Base 
belongs_to :user
…
End
Enter fullscreen mode Exit fullscreen mode

A belongs_to method I used is .association This method gives the object that is associated with the object the method is called on. For instance:

>> hang.user
=> #<User id: 5, username: "name1", password_digest: "$2a$10$...">
Enter fullscreen mode Exit fullscreen mode

I used this method in one of my views to display the username of the user that a hang belongs to. This is a useful and quick way to get information without having to write any custom queries.

A belongs_to method I didn’t use is .association= This method sets the association for the object it is called on. In my project I would use this method if I were to change the ownership of hangs. Let’s say that a user added a hang while accidentally logged into their friends account. I could add a route and controller method to change the ownership of a hang, and I might use @hang.user= new_user in that method.

Another belongs_to method I didn’t use is .build_association This method makes a new object from passed attributes and makes the object it is called on belong to the new object. As an example hang.build_user(username: "New user", password: "password") This makes a new user which the existing hang will belong to. This could be used in Strong Grips to allow a user to make a hang from the homepage, before they even make an account. The user would make the hang, and then be prompted to make an account. .build_user could then make their account which could be saved later.

I hope this post helped illustrate a few use cases for some of the Active Record associations methods. If you want to read the documentation about these methods it can be found here.

Top comments (0)