2009 07 21: Nested Attributes

  • 824 views
Uploaded on

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
824
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
12
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

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!