Simplified, I have the following relationships …
term has many swimming_classes and a swimming_class belongs to a term
swimming_pool has many swimming_classes and a swimming_class belongs to a swimming_pool
terms (date_from)------ swimming_classes (start_date) ------ swimming_pools (name)
We all speak Rails here, so just to clarify:
class Term
has_many :swimming_classes
has_many :swimming_pools, :through => :swimming_classes, :order => ‘name’
end
class SwimmingClass
belongs_to :term
belongs_to :swimming_pool
end
class SwimmingPool
has_many :swimming_classes, :order => ‘start_date DESC’
end
You probably noticed the association from Term to SwimmingPool that I tossed in there–be patient.
I want to order my output as …
Term (date_from DESC)
Swimming Pool (name ASC)
Swimming Class (start_date DESC)
Swimming Class (start_date DESC)
Swimming Class (start_date DESC)
Swimming Pool (name ASC)
Swimming Class (start_date DESC)
Swimming Class (start_date DESC)
Swimming Class (start_date DESC)
etc.
OK, so the view has
<li>Term: <%= term.date_from %>
<ul>
<% term.swimming_pools.each do |swimming_pool| -%>
<li>Pool: <%= swimming_pool.name %>
<ul>
<% swimming_pool.swimming_classes.each do |swimming_class| -%>
<li>Class starts: <%= swimming_class.start_date %></li>
<% end -%>
</ul>
</li>
<% end -%>
</ul>
</li>
<% end -%>
In my controller, I’m currently just getting terms (@current_term = Term.find_current_term)
and then pass that off to the view but this leaves me a lot of work to do to get the output I need. It seems I have to jump about a bit and it’s not nice…
If someone can just point me to the right place where I can read up on displaying data where data doesn’t run as nice as Invoice → Order → LineItem
CIA
-ants
In your controller, you seem to say that you’re getting a single term, but you said “date_from DESC”. Since it doesn’t make much sense to specify a sort order if there is only one, I’ll assume that you might get more than one (perhaps with a date range).
@terms = Term.commencing_between_now_and(params[:date])
and then in the model:
class Term
def self.commencing_between_now_and(future_date)
find(:all, :include => { :swimming_pools => :swimming_classes },
:conditions => ['date_from BETWEEN ? AND ?', Date.today, future_date])
end
end
Of course, you could add your own :order option to the find, but I think that it will work as-is.
I suspect that the view is quite a bit simpler than what you have now (since you said it was a “lot of work”). Generally, if you can put the data you want into a form that is inherently ordered (like an Array or an ActiveRecord::Association proxy), then your output becomes simpler. Even if you have to crunch the data a bit in the model (or the controller) first and a Hash is more natural, you can always loop in your view on:
<% my_data.keys.sort do |key| -%>
<%= my_data[key] %>
<% end -%>
or turn your hash into an association list (i.e., an array of pairs [key, value]) and pre-sort it before the view does its thing.
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com