On Aug 25, 10:39 am, Ar Chron <li...@ruby-forum.com> wrote: > Sandy wrote: > > On Aug 25, 4:00 am, Colin Law <clan...@googlemail.com> wrote: > >> >> Combining the lists and putting them in alphabetical order will show a > > >> Colin > > > Ok, Bob, > > > The problem now seems to be that there is a Household model which > > contains two fields, name and sname, each of which is the full name > > Yes, that would change the landscape a bit. Sounds like Sandy's > solution has the right of it by using @households to build @sorted, > thereby replicating the household record for each resident. > > -- > Posted viahttp://www.ruby-forum.com/.
I think that was the issue that Bob was trying to address, and I'm hoping it was due to his having inherited some legacy flat file database, and possilby not knowing how to normalize it, or being prevented from doing so by something else in the application which uses the households table. Otherwise, the application cries out for relationships between a Person model (a "belongs_to :household" relationship) and the Household model (a "has_many :people"). While this does complicate, slightly, the addition of people to households, it would be a better way to approach the problem, and it would allow for more than two "authorized receivers" per household (although I have no idea of what the business rules are for his application, and they could be... 2 and only 2 per household). Next, @sorted, as defined in the households_controller, does not replicate the household record for each "authorized receiver" (I prefer that term, rather than "resident", because a household could include many residents, only two of whom are authorized to receive food from the food bank). What @sorted does is to provide an array sorted by the names of the "authorized users" with the id of the relevant household record. With that id, one can use Household.find to obtain the relevant household record and then obtain any other field from that record. Of course, if any additional fields were needed in the view, they could be added to the hash in the @sorted array when it was created in the controller, thereby avoiding the need to do the lookups in the view and keeping the data access logic out of the view. While I did use @households in the households_controller, the only reason I did that was so that I could display both households and receivers in the same index.html.erb view, so that it was clear that the @sorted array was providing the "authorized users" in their sorted order with the id of the relevant household record. Otherwise, better practice would be to have households be a local variable within the controller. Sandy -- 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-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.