Master many-to-many associations with activerecord

Modeling many-to-many relationships between data entities in the ActiveRecord world isn’t always a straightforward task. Database instance Even if we have a well-defined ER diagram to work with, it’s not always clear which ActiveRecord associations we should be using and what the implications of our decision will be. Data recovery raid 0 There are two types of many-to-many relationships: transitive and intransitive. Data recovery tools In mathematics,

To put this into a data modeling context, a relationship between two entities is transitive if it can be best expressed through the introduction of one or more other entities.


Database programming languages So, for instance, it’s easy to see that a Buyer buys from many Sellers while a Seller sells to many Buyers. Super 8 database However, the relationship is not fully expressed until we start adding entities such as Product, Payment, Marketplace and so on. 5 databases Such relationships can be called transitive many-to-many as we rely on the presence of other entities to fully capture the semantics of the relationship. H data recovery registration code Luckily, ActiveRecord allows us to model such relationships with ease. Database link Let’s start by looking at the simplest ActiveRecord many-to-many associations and work our way up. Database google Intransitive Associations

This is the simplest many-to-many association. Database update Two models are associated by simple virtue of their existence. Data recovery news A Book can be written by many authors and an Author may write many books. Data recovery austin It is a direct association and there is a direct dependency between the two models. Data recovery micro sd card We can’t really have one without the other. Database relationship diagram In ActiveRecord this can easily be modeled with the has_and_belongs_to_many (HABTM) association. Data recovery services near me We can create the models and migrations for this relationship in Rails by running the following commands: rails g model Author name:string

We can now, among other things, access: a book’s Authors, all Books written by an Author and all Authors that have written a specific book: moby.authors

A transitive association that can be best described with the addition of a single extra model. Database worksheet Take the example of a Student. Database constraints A Student can be taught by many Tutors and a Tutor can teach many Students, but we can’t fully express the relationship unless we include another entity: Class (to avoid Ruby reserved-name conflicts, let’s name it Klass) rails g model Student name:string

We can say that a Student is taught through attending Klasses and that a Tutor teaches Students through giving Klasses. Database vault The word through is important here, as we use the same term in ActiveRecord to define the association: class Student < ApplicationRecord

As well as the usual simple finds we can also create some more complex queries: Student.find_by(name: ‘Bart Simpson’).tutors # find all Bart’s tutors

Student.joins(:tutors).joins(:klasses).where(klasses: {subject: ‘Maths’}, tutors: {name: ‘Mrs Krabapple’}).distinct.map {|x| puts x.name} # get all students who attend Maths taught by Mrs Krabapple

As in most cases of mono-transitive associations, the existing model names reflect the association implicitly (i.e. Qmobile data recovery software X has_many Z through Y), there is no need for us to do anything more and ActiveRecord will model our association perfectly. Tally erp 9 data recovery software Multi-transitive Associations

A multi-transitive association is one that can be expressed best through many other models. 7 databases in 7 weeks We as Developers, for example, are associated with many software Communities. 3 database models Our association, though, takes many forms: we may contribute code, post at forums, attend events, and many others. Database programmer salary Each Developer is associated with a Community in their own way through specific actions. Database developer salary Let’s pick three of these actions for our example:

The next step in our modeling process is to define the data entities (models) which help realize these actions (associations). Data recovery agent For our example, we can safely come up with: Association

We then can define the associations between our models. Data recovery usb flash drive At this point we may be tempted to use the same technique we used in the mono-transitive example and repeat the has_many..through invocation for each association: class Developer < ApplicationRecord

However, this won’t work as ActiveRecord will try to infer the name of the association’s source model from the association name (e.g appearance) and it will fail. Data recovery windows For this reason we need to specify the source model name using the :source option: class Developer < ApplicationRecord

As you may have noticed, on the Community model we are changing the names of some associations to reflect their nature from this side of the relationship. Database definition For instance, a Developer makes appearances at events, while a Community hosts events. Data recovery definition A Developer posts at forums, while a Community fosters discussions at forums. Database or database This way, we are ensuring that our method names (that AR will dynamically create based on our associations) are meaningful and clear.

We can now create some events, forums, and repos: Repo.create url: ‘www.gitlab.com/342’, comment: ‘ruby code’, developer: devs[0], community: comms[0]

Community.find_by(name: ‘rails’).hostings + Community.find_by(name: ‘rails’).discussions + Community.find_by(name: ‘rails’).contributions # get all events, forums and repositories for a specific community

Developer.select(‘distinct developers.name’).joins(:repos).joins(:events).joins(:forums) # find developers who have appeared in Events, contributed to Repos and chatted on Forums, for any Community

banner