Short presentation takes you the basics of Rails Model Associations by constructing a simple app, Timber: the dating app for Lumberjacks and Lumberjills. Code can be found on github: https://github.com/PareidoliaX/timber
2. Four Take Homes
1. Associations are not database relationships but
dependent on them.
2. There is always a belongs_to side of the association.
3. The table of the model of the belongs_to side of the
association holds the information about the
relationship.
4. belongs_to works in migrations and generators.
3. Timber Iteration 1
Requirements
4 Tracks Lumberjacks and Lumberjills
4 A Lumberjack or Lumberjill can choose on true love
4 A Lumberjack or Lumberjill's profile shows all their
admires
6. LumberJack Migration
class CreateLumberjacks < ActiveRecord::Migration
def change
create_table :lumberjacks do |t|
t.string :name
t.belongs_to :lumberjill, index: true
t.timestamps
end
end
end
7. LumberJill Migration
class CreateLumberjills < ActiveRecord::Migration
def change
create_table :lumberjills do |t|
t.string :name
t.belongs_to :lumberjack, index: true
t.timestamps
end
end
end
8. LumberJill Model
class Lumberjill < ActiveRecord::Base
belongs_to :lumberjack
has_many :lumberjacks
end
9. LumberJack Model
class Lumberjack < ActiveRecord::Base
belongs_to :lumberjill
has_many :lumberjills
end
10. Limitations of this solution
+ Only one true love possible, can't handle complex relationships.
11. Four Take Homes
1. Associations are not database relationships but
dependent on them.
2. There is always a belongs_to side of the association.
3. The table of the model of the belongs_to side of the
association holds the information about the
relationship.
4. belongs_to works in migrations and generators.