Re: [ranch] 11: Make sure the bench name list is uniq.

Nicolas Desprès <nicolas.despres@gmail.com> disait le 11/09/05 :
Index: web/ranch/app/controllers/benches_controller.rb --- web/ranch/app/controllers/benches_controller.rb (revision 10) +++ web/ranch/app/controllers/benches_controller.rb (working copy) @@ -4,9 +4,11 @@
def list @project = Project.find_by_id(@params[:project_id]) - @benches = Bench.find_all_by_project_id(@project.id).sort do |a, b| - a.name <=> b.name + @benches_name = Bench.find_all_by_project_id(@project.id).collect do |x| + x.name end + @benches_name.uniq! + @benches_name.sort! end
You should rely on SQL, which is much more faster, I think. @benches = Bench.find_by_sql( "SELECT name FROM benches WHERE project_id = #{@project.id} GROUP BY name ORDER BY name") or: @benches = Bench.find :all, :conditions => ["project_id = ?", @project.id] :order => "name" However, I didn't found how to put DISTINCT with find. Hurray for Rails !
participants (1)
-
olivier