two many to many timber app

17
Timber App: Two Many T o Many Ruby Rails Associa tions @PareidoliaX

Upload: alexander-miller

Post on 07-Jul-2015

272 views

Category:

Internet


1 download

DESCRIPTION

Building Timber with Many To Many Associations

TRANSCRIPT

Page 1: Two Many To Many Timber App

Timber App: Two Many To Many

Ruby Rails Associations@PareidoliaX

Page 2: Two Many To Many Timber App

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

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

Timber Iteration 2NEW Requirement

4 A Lumberjack or Lumberjill can make a hot list

Page 5: Two Many To Many Timber App

Today's Association:4 has_many through:

4 has_and_belongs_to_many

Page 6: Two Many To Many Timber App

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

Things will a little be weird

Page 8: Two Many To Many Timber App

LumberJack's use has_and_belongs_to_many

They are simpler and less flexible.

Page 9: Two Many To Many Timber App

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

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

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

LumberJill's use has_many :throughThey are more flexible and complicated.

Page 13: Two Many To Many Timber App

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

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

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

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

Find Me4 Twitter: @pareidoliax

4 Website: pareidoliax.ca

4 Github: pareidoliax

4 email: [email protected]