2009 07 21: Nested Attributes
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

2009 07 21: Nested Attributes

on

  • 1,927 views

How to use simple forms with nested models and single access points into web api's without the view or controller logic getting messy. Demonstration of the Rails 2.3 accepts_nested_attributes_for ...

How to use simple forms with nested models and single access points into web api's without the view or controller logic getting messy. Demonstration of the Rails 2.3 accepts_nested_attributes_for feature
Video: http://blip.tv/file/3957941

Statistics

Views

Total Views
1,927
Views on SlideShare
1,820
Embed Views
107

Actions

Likes
0
Downloads
12
Comments
0

6 Embeds 107

http://thetechnologist.tv 65
http://www.thetechnologist.tv 33
http://www.slideshare.net 4
http://www.linkedin.com 2
https://www.linkedin.com 2
http://www.apurva.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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!