Accept_nested_attributes, reject_if doesn't work

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?

Looking at
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.htmlI
think accepts_nested_attributes expects an #{association}_attributes
key/value pair to be passed
as an attribute to the parent object, in this case Post.

doing post.tags.create doesn’t pass by accepts_nested_attributes_for but
the
normal Tag.create.

On Fri, Feb 11, 2011 at 9:21 PM, Mauro [email protected] wrote:

irb(main):003:0>
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs