On Oct 1, 2010, at 2:19 AM, Bob Smith 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     <[email protected]>

While I'm not contradicting the responses from Michael and pepe, I'm going to lead you a different way.

First, I'll suggest that you don't have this level of code in your view. Put a method on your Household model that returns the people in the proper order. Your view code then becomes:

<%= render :partial => 'person', :collection => @household.people_by_birthday %>

(Which in later version of Rails can be
 just <%= render @household.people_by_birthday %> )

Second, use one of the methods given by Michael or pepe to keep persons having invalid (or missing) birthdays from being created.

Finally, as a possible implementation of the #people_by_birthday method (note that I'm assuming that there's a #name method on which to sort the persons having no birthday):

class Household
  def people_by_birthday
    list = self.people.partition {|person| person.birthday.blank?}
    list[1].sort_by(&:birthday) + list[0].sort_by(&:name)
  end
end

-Rob


Rob Biedenharn          
[email protected]     http://AgileConsultingLLC.com/
[email protected]               http://GaslightSoftware.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 [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.

Reply via email to