Skip to content

Instantly share code, notes, and snippets.

@partydrone
Created March 23, 2009 22:49
Show Gist options
  • Save partydrone/83829 to your computer and use it in GitHub Desktop.
Save partydrone/83829 to your computer and use it in GitHub Desktop.
Given the following associations: [ download_types --> downloads --> downloads_products <-- products ], I create a query (in download.rb) that will list all downloads for a given product ordered by download type.
<% @downloads.group_by(&:download_type).each do |download_type, downloads| -%>
<h3><%= download_type.name.pluralize %></h3>
<ul>
<%= render :partial => 'support/download', :collection => downloads %>
</ul>
<% end -%>
class Download < ActiveRecord::Base
belongs_to :download_type
has_and_belongs_to_many :products
def self.get_by_product(id)
all(:include => :download_type, :joins => :products, :conditions => ['products.id = ?', id], :order => 'download_types.position, title')
end
end
class SupportController < ApplicationController
def downloads
begin
@downloads = Download.get_by_product(params[:id])
render :partial => 'support/downloads'
rescue ActiveRecord::RecordNotFound
render :text => ''
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment