> On Feb 3, 2018, at 2:54 PM, fugee ohu <fugee...@gmail.com> wrote:
> 
> 
> 
> On Friday, January 12, 2018 at 7:06:12 PM UTC-5, fugee ohu 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
> 
> Naming conventions for the join table are the same as for  
> has_and_belongs_to_many ?

There are no requirements on this. The join table should be named for the join 
model, not the sides of the relationship. The reason that the HABTM naming 
convention exists at all is because there is no model in the middle, just the 
table. That means that the table name must be inferred from the sides of the 
relationship, since there would be no other way to configure that.

Often in this area, you will see a join table name that means something 
specific to the relationship. So between User and Group, you might see 
Membership, and that might mean that User has:

has_many :memberships
has_many :groups, through: :memberships

and Group has:

has_many :memberships
has_many :users, through: :memberships

But you may want to say @group.members because it expresses the semantics of 
the relationship better:

has_many :members, through: :memberships, class_name: 'User', foreign_key: 
:user_id

This sort of relationship is endlessly configurable and flexible, which is 
another reason why it is preferred over HABTM.

Walter

> 
> -- 
> 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+unsubscr...@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/ddab7848-1da5-44fc-a748-63ade27a54f7%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+unsubscr...@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/8A39B358-E7EA-4922-86AC-263872ABB96B%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to