Ruby on Rails Friday, January 12, 2018

This is a basic part of what is called a many-to-many relationship, which is what you describe here. These relationships always rely on a third table in any relational database-backed application (i.e. this is not specific to Rails).

There are two ways to model this relationship in Rails. The first (and oldest) is the has_and_belongs_to_many relationship, which uses a simple table consisting of two columns (accessory_id and product_id), and does a "dumb" join between the two models. Nothing (besides the simple fact of bi-directional relation) is stored in the middle table.

The second approach, which has been recommended in preference to the "dumb join" for many years, is the has_many through relationship. That also uses a third table, but that table backs a full ActiveRecord model. It would have an id, along with the product_id and accessory_id, and could include as many other columns as you may need in order to properly model the relationship between the other two objects. For example, you could "decorate" the join between the two main objects with more specific details about their relationship.

Say if you had a Person, Team, and Membership model, you could use the Membership to carry the date the person joined the team, or their role on the team. Your models would look like this:

class Person < ActiveModel
has_many :memberships
has_many :teams, through: :memberships
end

class Team < ActiveModel
has_many :memberships
has_many :people, through: :memberships
end

class Membership < ActiveModel
belongs_to :person
belongs_to :team
end

If you have any thoughts at all that you may want to have a richer connection between products and accessories, then by all means use the has_many through relationship.

Walter

> On Jan 12, 2018, at 7:06 PM, fugee ohu <fugee279@gmail.com> wrote:
>
> Let's say I have a table of products and a table of accessories Each accessory can have many products that it's compatible with while each product has many accessories available for it It In the problem I'm trying to solve accessories cannot belong to products and products cannot belong to accessories So, unless I was to create a third table with fields for product_id and accessory_id, how could I allow each to have many of the other Do I have to create the third table? Thanks in advance
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e161bc52-f089-493a-8d12-00f597726217%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/47985E55-2BAA-4358-8EC4-C7F8AB799489%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment