Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Workshop 2

Mike Skalnik
February 27, 2012

Rails Workshop 2

Mike Skalnik

February 27, 2012
Tweet

More Decks by Mike Skalnik

Other Decks in Programming

Transcript

  1. Ruby
    Rails
    ON
    2
    Monday, February 27, 12

    View Slide

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

    View Slide

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

    View Slide

  4. ActiveRecord
    Associations
    Monday, February 27, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  8. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  12. has_many :through
    has_and_belongs_to
    vs
    Monday, February 27, 12

    View Slide

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

    View Slide

  14. 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?

    View Slide

  15. vs
    Monday, February 27, 12

    View Slide

  16. 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!

    View Slide

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

    View Slide

  18. $ terminal_time
    Monday, February 27, 12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. @
    Web
    Tech
    Innovators
    Monday, February 27, 12

    View Slide

  36. ATLRUG
    Monday, February 27, 12

    View Slide

  37. THANKS
    Monday, February 27, 12

    View Slide