1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Revert "Merge pull request #33385 from lanzhiheng/add-example-for-has-and-belongs-to-many-association"

This reverts commit 3c8c410012, reversing
changes made to daee94da99.

We have this information below in the reference section for
has_and_belongs_to_many.
This commit is contained in:
Rafael Mendonça França 2018-07-18 16:31:42 -04:00
parent 5df23a04a3
commit dc1c679d89
No known key found for this signature in database
GPG key ID: FC23B6D0F1EEE948

View file

@ -365,22 +365,6 @@ class CreateAssembliesAndParts < ActiveRecord::Migration[5.0]
end
```
As you can see we don't create the relevant model for `assemblies_parts` table, So We can't create the many-to-many record like this
``` ruby
AssemblyPart.create(assembly: @assembly, part: @part) # => NameError: uninitialized constant AssemblyPart
```
If you want to create the relevant many-to-many record, you can use the below code
``` ruby
@assembly.parts << @part
# Or
@part.assemblies << @assembly
```
They are equivalent.
### Choosing Between `belongs_to` and `has_one`
If you want to set up a one-to-one relationship between two models, you'll need to add `belongs_to` to one, and `has_one` to the other. How do you know which is which?