On Oct 29, 2009, at 6:07 PM, Anthony G. wrote:
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.
Swimming Class (start_date DESC)
Swimming Class (start_date DESC)
etc.
OK, so the view has
<% @terms.each do |term| -%>
- Term: <%= term.date_from %>
<% term.swimming_pools.each do |swimming_pool| -%>
- Pool: <%= swimming_pool.name %>
<% swimming_pool.swimming_classes.each do |
swimming_class| -%>
- Class starts: <%= swimming_class.start_date %>
<% end -%>
<% end -%>
<% end -%>
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 B. http://agileconsultingllc.com
[email protected]