2009 07 21: Nested Attributes

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    2009 07 21: Nested Attributes - Presentation Transcript

    1. Wolfram Arnold www.rubyfocus.biz Nested Attributes
        • CRUD
        • on multiple models
        • without crud
    2. Associations
      • class Pirate < ActiveRecord::Base has_one :ship end
      • class Ship < ActiveRecord::Base belongs_to :pirate end
    3. User Interface?
      • Form with pirate and ship fields
        • form_for :pirate do |pf| pf.text_field :catchphrase pf.fields_for :ship do |sf| sf.text_field :name end # params[pirate][ship] # Note: broken! end
    4. Controller Interface?
        • def create @pirate = Pirate.new(params[:pirate]) @ship = Ship.new(params[:pirate][:ship]) @ship.pirate = @pirate if @pirate.save && @ship.save ... else # what about rollback? end end
    5. Demo
    6. Fat model/skinny controller
      • Logic in the model, not views/controllers
      • Assignments
        • class Pirate < ActiveRecord::Base def ship_attributes=(attrs) ... end end
      • Validations handled in model, incl. child models
    7. Saving—when?
      • :autosave => true flag, new in Rails 2.3!
      • Automatically turned on for accepts_nested_attributes_for
      • Parent will save all children
        • New parent, modified children
        • Existing parent, new/modified children
        • As a transaction—rollback on validation failure
        • Cascaded error messages
      • Removes a lot of confusion
      • Demo
    8. Web Service API
      • PirateController and ShipController?
      • Not advised if
        • Objects have a relationship, e.g. has_one association
        • Dependencies
        • Business logic rules/validations
      • Better single API with nested attributes
        • Simpler client code
        • Less network traffic
    9. References
      • http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#M002132
      • http://github.com/h-lame/parental_control
      • http://railscasts.com/episodes/75-complex-forms-part-3
      • Advanced Rails Recipes, Pragmatic Programmer Series
    10. Wolfram Arnold www.rubyfocus.biz
        • Thank you!
    SlideShare Zeitgeist 2009

    + Wolfram ArnoldWolfram Arnold Nominate

    custom

    531 views, 0 favs, 2 embeds more stats

    How to use simple forms with nested models and sing more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 531
      • 495 on SlideShare
      • 36 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 6
    Most viewed embeds
    • 19 views on http://thetechnologist.tv
    • 17 views on http://www.thetechnologist.tv

    more

    All embeds
    • 19 views on http://thetechnologist.tv
    • 17 views on http://www.thetechnologist.tv

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories