Skip to content

Instantly share code, notes, and snippets.

@leepfrog
Created January 11, 2010 03:30
Show Gist options
  • Save leepfrog/273972 to your computer and use it in GitHub Desktop.
Save leepfrog/273972 to your computer and use it in GitHub Desktop.
class Blog < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, :allow_destroy => true
before_validation :set_self_as_parent
def set_self_as_parent
self.posts.each |post|
post.blog = self
end
end
end
class Post < ActiveRecord::Base
belongs_to :blog
validates_presence_of :blog
end
a=Blog.new
a.posts_attributes={0 => {:text => "my cool post"}}
a.save
a.errors.full_messages => "Posts blog can't be blank"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment