Skip to content

Instantly share code, notes, and snippets.

@contentfree
Created March 20, 2009 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save contentfree/82527 to your computer and use it in GitHub Desktop.
Save contentfree/82527 to your computer and use it in GitHub Desktop.
# The models
class User < ActiveRecord::Base
has_many :things
accepts_nested_attributes_for :things
end
class Thing < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id
validates_presence_of :name # For clarity below
end
# Trying to create a new user with a new thing
u = User.new(:things_attributes => [{:name => 'thing'}])
u.save #=> false
u.errors.full_messages #=> ["Things user can't be blank"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment