38. JavaScript / Assignments
• As a user I want to show errors
on my author creation form
before I submit the form so I
know if the name is a valid
one.
• As a user I want to be able to
preview my post with a
separate button so I can see
how it will look before I actually
save the post.
51. Associations / One to many
// create migration
rails g migration AddAuthorIdToPost author_id:integer
// run migration
rake db:migrate
52. Associations / One to many
// generated code
class AddAuthorIdToPost < ActiveRecord::Migration
def change
add_column :posts, :author_id, :integer
end
end
53. Associations / One to many
// add model code
class Post < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :posts
end
54. Associations / One to many
// add model code
class Post < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_one :post
end
55. Associations / One to many
// creating associations
author = Auther.create name: ‘Joost’
author.posts.create title: ‘railsgirls #4’, body: ‘current
course’
Post.create title: ‘railsgirls #5’, body: ‘new course’,
author: author
57. Associations / Many to many
Post
title
body
author_id
Category
name
0..*
PostCategory
post_id
category_id
1
0..*
1
58. Associations / Many to many
// create migration
rails g migration create_categories_posts post_id:integer
category_id:integer
// run migration
rake db:migrate
59. Associations / Many to many
// generated code
class CreateCategoriesPosts < ActiveRecord::Migration
def change
create_table :categories_posts do |t|
t.integer :post_id
t.integer :category_id
end
end
end
60. Associations / Many to many
// add model code
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
end
61. Associations / Many to many
// creating associations
post = Post.create title: ‘railsgirls #4’, body: ‘current
course’
post.categories = [Category.create, Category.create]
post.categories << Category.create name: ‘new category’
64. Associations / Dependencies
// setting up dependencies
class AuthorsController < ApplicationController
def destroy
@author.destroy
respond_to do |format|
format.html { redirect_to authors_url, notice: 'Author was
successfully destroyed.' }
format.json { head :no_content }
end
end
end
65. Associations / Dependencies
// setting up dependencies
class AuthorsController < ApplicationController
def destroy
@author.posts.each do |post|
post.destroy
end
@author.destroy
respond_to do |format|
format.html { redirect_to authors_url, notice: 'Author was
successfully destroyed.' }
format.json { head :no_content }
end
end
end
66. Associations / Dependencies
// setting up dependencies
class Author < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
73. JavaScript / Assignments
• As an author I want to be able
to create a post from my page
so that the post will be
attached to my name.
• As an author I want to be able
to attach and detach categories
on my posts so they are nicely
labelled.