Ruby on Rails Friday, October 1, 2010

On 1 October 2010 07:19, Bob Smith <bsm2th@gmail.com> wrote:
> Is there any way to delete blank records as part of the validation??

What do you mean "as part of the validation"? Which validation? What
records do you want to delete? Why allow them to save if they're not
valid?

There are lots of ways you can stop the errors though - it just
depends which direction you want to approach it:
* You could add validation to the Person model to make birthday mandatory.
* You could add a method to Person called "birthday" which returns
the birthday attribute or a dummy date (you could call this method
"birthday_for_sort" - to make it clear what its purpose is, and to
maintain the AR method for )
* you could remove records from the people collection before the sort_by:
@household.people.delete_if { |p| p.birthday.blank? }.sort_by(&:birthday)
* you could add a fake dob value before the sort_by:
@household.people.each { |p| p.birthday = "1 Jan 1900" if
p.birthday.bllank? }.sort_by(&:birthday)

and more ways to crack the nut...
Some of them smell more than others - depends, as ever, on what you
actually want to achieve... but maybe you can get some idea of how to
sort your problem.

HTH

--
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