On Friday, January 12, 2018 at 9:02:27 PM UTC-5, Walter Lee Davis wrote:
> 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
> 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
> class Team < ActiveModel
> has_many :memberships
> has_many :people, through: :memberships
> class Membership < ActiveModel
> belongs_to :person
> belongs_to :team
> 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.
> > 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
> > To post to this group, send email to rubyonra...@googlegroups.com
> > To view this discussion on the web visit
> > For more options, visit https://groups.google.com/d/optout.
So if I'm using the second approach the has_many_through then how do i
create a new record?
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 post to this group, send email to firstname.lastname@example.org.
To view this discussion on the web visit
For more options, visit https://groups.google.com/d/optout.