On Fri, Jul 31, 2009 at 13:39, Murray Steele <murray.ste...@gmail.com>wrote:
> I have to agree with Mislav and Duncan here, if firstname should be unique, > a finder is not the place to enforce that. Those 2 other "mislav" users > should not be in the database in the first place! There may be a compelling > use-case for a find(:only/unique/make_sure_there_is_only_one_result), but > late detection of invalid data isn't it. > To further explain my reasoning: To me, `first()` is a method I almost exclusively use in the console. I think of it like "get me the first record that matches some conditions or scope I specify". If there was a `unique` or `single` method, would the implementation be much different? For production code, I use dynamic finders against columns that are constrained with a unique key—e.g.: find(1) find_by_username('mislav') The implementation for these is (I believe) identical to `first`. I only use `first` in combination with ordering to say "get me the user with most comments" or "get me the most recent comment", but this is only for presentational purposes and you can agree that such a use case doesn't come often. But, it's obvious that it's completely wrong to use `first` or `find_by` on some column(s) that are *not* under a unique constraint. This should always be used: find_all_by_username('mislav') I guess that all I'm trying to say can be summed up as: 1. if the column is unique-constrained, you *should *use find_by_column (or `first` with conditions or scope); 2. if the column is *not* unique, you *must* use find_all_by_column (or alternatives); 3. there's no need for an extra method. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---