Two Many To Many Timber App
-
Upload
alexander-miller -
Category
Internet
-
view
274 -
download
1
description
Transcript of Two Many To Many Timber App
![Page 1: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/1.jpg)
Timber App: Two Many To Many
Ruby Rails Associations@PareidoliaX
![Page 2: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/2.jpg)
Three Take Homes1. Both has_many through: and has_and_belong_to_many
require a join table.
2. has_many through is more versitile than has_and_belong_to_many but requires creating an additional model.
3. Since there is no model has_and_belongs_to_many requires you to manipulate the relationship as a ActiveRecord collection.
![Page 3: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/3.jpg)
Timber Iteration 1Requirements
4 Tracks Lumberjacks and Lumberjills
4 A Lumberjack or Lumberjill can choose one true love
4 A Lumberjack or Lumberjill's profile shows all their admires
![Page 4: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/4.jpg)
Timber Iteration 2NEW Requirement
4 A Lumberjack or Lumberjill can make a hot list
![Page 5: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/5.jpg)
Today's Association:4 has_many through:
4 has_and_belongs_to_many
![Page 6: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/6.jpg)
Based on the Many to Many database relationship4 Requires a Join Table which saves the belongs_to
data.
![Page 7: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/7.jpg)
Things will a little be weird
![Page 8: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/8.jpg)
LumberJack's use has_and_belongs_to_many
They are simpler and less flexible.
![Page 9: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/9.jpg)
LumberJack Join Table Migrationclass LumberjacksLumberjills < ActiveRecord::Migration def change create_table :lumberjacks_lumberjills, id: false do |t| t.belongs_to :lumberjack, index: true t.belongs_to :lumberjill, index: true end endend
![Page 10: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/10.jpg)
LumberJack Modelclass Lumberjack < ActiveRecord::Base belongs_to :lumberjill has_many :lumberjills
has_and_belongs_to_many :hotties, class_name: 'Lumberjill'end
![Page 11: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/11.jpg)
LumberJack Seedputs "Adding hotties to lumberjacks"lumberjacks.each do |lumberjack| rand(2..7).times do lumberjack.hotties << lumberjills.sample end print lumberjack.hotties.pluck(:id).to_send
![Page 12: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/12.jpg)
LumberJill's use has_many :throughThey are more flexible and complicated.
![Page 13: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/13.jpg)
LumberJill Join Table Migrationclass CreateHots < ActiveRecord::Migration def change create_table :hots do |t| t.belongs_to :lumberjack, index: true t.belongs_to :lumberjill, index: true
t.timestamps end endend
![Page 14: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/14.jpg)
LumberJill Modelclass Lumberjill < ActiveRecord::Base belongs_to :lumberjack has_many :lumberjacks
has_many :hots has_many :hotties, through: :hots, source: :lumberjackend
![Page 15: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/15.jpg)
LumberJill Seedputs "Adding hotties to lumberjills"lumberjills.each do |lumberjill| rand(2..7).times do lumberjill.hots.create lumberjack: lumberjacks.sample end print lumberjill.hotties.pluck(:id).to_send
![Page 16: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/16.jpg)
Three Take Homes1. Both has_many through: and has_and_belong_to_many
require a join table.
2. has_many through is more versitile than has_and_belong_to_many but requires creating an additional model.
3. Since there is no model has_and_belongs_to_many requires you to manipulate the relationship as a ActiveRecord collection.
![Page 17: Two Many To Many Timber App](https://reader034.fdocuments.net/reader034/viewer/2022052602/559b57401a28ab26298b4744/html5/thumbnails/17.jpg)
Find Me4 Twitter: @pareidoliax
4 Website: pareidoliax.ca
4 Github: pareidoliax
4 email: [email protected]