Created
March 23, 2009 22:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @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 -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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