Ruby on Rails Friday, October 1, 2010

You could use 'accepts_nested_attributes_for' in your parent model:

accepts_nested_attributes_for :children_models,
:allow_destroy => true,
:reject_if => proc {|attributes|
attributes['your_attribute'].blank?}

The 'reject_if' is what does the trick.

On Oct 1, 2:19 am, Bob Smith <bsm...@gmail.com> wrote:
> In my view, I do a
>
> <%= render :partial => 'person', :collection =>
> @household.people.sort_by(&:birthday) %>
>
> household has a has_many relationship with person, and this works fine
> until...
>
> an errant user creates a new person and tries to save without filling
> the fields(day, month, and year). After this the birthday field is
> created and fails horribly. Is there any way to delete blank records
> as part of the validation??
>
> Thanks
>
> Bob     <bsm...@gmail.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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment