Slide 1

Slide 1 text

Ruby Rails ON 2 Monday, February 27, 12

Slide 2

Slide 2 text

About Me Mike Skalnik github.com/skalnik @skalnik Monday, February 27, 12

Slide 3

Slide 3 text

Where did we stop? We have posts! Monday, February 27, 12

Slide 4

Slide 4 text

ActiveRecord Associations Monday, February 27, 12

Slide 5

Slide 5 text

4 Kinds Monday, February 27, 12 has_one, has_many, habtm/has_many :through, belongs_to

Slide 6

Slide 6 text

has_one Georgia Tech A School Buzz Mascot has_one Monday, February 27, 12

Slide 7

Slide 7 text

has_one class School < ActiveRecord::Base has_one :mascot end Monday, February 27, 12

Slide 8

Slide 8 text

has_many id title body 1 First Post! … 2 The Value of… … id author body post_id 1 Mike … 1 2 Eric … 1 3 Kyle … 2 Monday, February 27, 12

Slide 9

Slide 9 text

has_many class Post < ActiveRecord::Base has_many :comments end Monday, February 27, 12

Slide 10

Slide 10 text

belongs_to class Comment < ActiveRecord::Base belongs_to :post end Monday, February 27, 12

Slide 11

Slide 11 text

belongs_to class Mascot < ActiveRecord::Base belongs_to :school end Monday, February 27, 12

Slide 12

Slide 12 text

has_many :through has_and_belongs_to vs Monday, February 27, 12

Slide 13

Slide 13 text

expose join table hide join table vs Monday, February 27, 12 Exposed as a model

Slide 14

Slide 14 text

class Category < ActiveRecord::Base has_many :categorizations has_many :posts, :through => categorizations end class Categorization < ActiveRecord::Base belongs_to :post belongs_to :category end class Post < ActiveRecord::Base has_many :categorizations has_many :categories, :through => categorizations end Monday, February 27, 12 Allows for validations, along with attaching data to the association. How about Users with Ratings & Songs?

Slide 15

Slide 15 text

vs Monday, February 27, 12

Slide 16

Slide 16 text

class Post < ActiveRecord::Base has_and_belongs_to_many :categories end class Category < ActiveRecord::Base has_and_belongs_to_many :posts end Monday, February 27, 12 No touching the table, just a link. Still need to write migration though!

Slide 17

Slide 17 text

Usage @post.comments #=> [#, …] @post.categories #=> [#, …] @comment.post #=> # @school.mascot #=> # Monday, February 27, 12

Slide 18

Slide 18 text

$ terminal_time Monday, February 27, 12

Slide 19

Slide 19 text

ActiveRecord Validations ✖ ✓ Monday, February 27, 12 Not a full list, but the more common ones

Slide 20

Slide 20 text

validates :terms, :acceptance => true Monday, February 27, 12 Accept TOS

Slide 21

Slide 21 text

validates :password, :confirmation => true Monday, February 27, 12 confirm password

Slide 22

Slide 22 text

validates :username, :exclusion => { :in => [‘user’] } Monday, February 27, 12 reserved usernames

Slide 23

Slide 23 text

validates :email, :format => { :with => /.*@.*/ } Monday, February 27, 12 validate email format

Slide 24

Slide 24 text

validates :size, :inclusion => { :in => [‘small’, …] } Monday, February 27, 12 size is in list

Slide 25

Slide 25 text

validates :name, :length => { :minimum => 2, :maximum => 500, :in => 2..500, :is => 12 } Monday, February 27, 12 Lots of options

Slide 26

Slide 26 text

validates :points, :numericality => true Monday, February 27, 12

Slide 27

Slide 27 text

validates :email, :presence => true Monday, February 27, 12 Ensure it’s there

Slide 28

Slide 28 text

validates :email, :uniqueness => true Monday, February 27, 12 Ensure they haven’t registered before

Slide 29

Slide 29 text

Usage @post.save #=> false if not valid @post.valid? Monday, February 27, 12

Slide 30

Slide 30 text

More Resources Monday, February 27, 12 Not a full list, but the more common ones

Slide 31

Slide 31 text

RailsGuides http://guides.rubyonrails.org Monday, February 27, 12 Not a full list, but the more common ones

Slide 32

Slide 32 text

RailsCasts http://railscasts.com Monday, February 27, 12 Not a full list, but the more common ones

Slide 33

Slide 33 text

Ruby Toolbox http://ruby-toolbox.com Monday, February 27, 12 Not a full list, but the more common ones

Slide 34

Slide 34 text

Me [email protected] Monday, February 27, 12 Not a full list, but the more common ones

Slide 35

Slide 35 text

@ Web Tech Innovators Monday, February 27, 12

Slide 36

Slide 36 text

ATLRUG Monday, February 27, 12

Slide 37

Slide 37 text

THANKS Monday, February 27, 12