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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jim Ruther Nill
2011-Feb-11 13:32 UTC
Re: accept_nested_attributes, reject_if doesn''t work.
Looking at http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html <http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html>I 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 <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.