class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments, :dependent => :destroy
has_many :tags
accepts_nested_attributes_for :tags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? or v.nil?
} }
end
rails c
irb(main):001:0> post=Post.first
=> #<Post id: 1, name: “prova”, title: “titolo prova”, content: “prova
1”, created_at: “2011-02-01 10:03:10”, updated_at: “2011-02-01
10:03:10”>
irb(main):002:0> post.tags.create()
=> #<Tag id: 5, name: nil, post_id: 1, created_at: “2011-02-11
13:19:22”, updated_at: “2011-02-11 13:19:22”>
irb(main):003:0>
irb(main):003:0> post.valid?
=> true
Tags has blank or nil attributes but it is saved.
Why I have put :reject_if => proc { |attrs| attrs.all? { |k, v|
v.blank? or v.nil? } } for tags?