# Best Practice For Displaying Data

**URL:** <https://www.ruby-forum.com/t/best-practice-for-displaying-data/178157>\
**Category:** Rails\
**Created:** [October 29, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/best-practice-for-displaying-data/178157 "2009-10-29T23:09:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [October 29, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/best-practice-for-displaying-data/178157/1 "2009-10-29T23:09:05Z")

</div>

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)

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.

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

---

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [October 29, 2009, 11:28pm UTC](https://www.ruby-forum.com/t/best-practice-for-displaying-data/178157/2 "2009-10-29T23:28:08Z")

</div>

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)
> 
> 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.
> 
> 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…

No you don’t. Just use :include and/or :joins to get all the Terms,  
Pools, and Classes in one big query.

> 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

But your data _does_ run as nice as Invoice → Order → LineItem. You  
just need to use the :order options in your find statement.

With that in mind, then, your code will be something like the following  
(untested, and I’m not sure about the :order syntax):  
@terms = Term.find(:all, :include =\> {:swimming\_pools =\>  
:swimming\_classes}, :order =\> ‘terms.date\_from desc,  
swimming\_pools.name, swimming\_class.start\_date desc’}

> CIA
> 
> ## -ants

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [October 30, 2009, 2:24am UTC](https://www.ruby-forum.com/t/best-practice-for-displaying-data/178157/3 "2009-10-30T02:24:36Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![acgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@acgreen](https://www.ruby-forum.com/u/acgreen)\
**Post date:** [October 30, 2009, 9:28am UTC](https://www.ruby-forum.com/t/best-practice-for-displaying-data/178157/4 "2009-10-30T09:28:23Z")

</div>

Doh, I didn’t think of linking pools to terms in the Term model.

that was what I was missing. Your other points are correct but in the  
example I gave, there is only one current term (so the :order date\_from  
isn’t needed) but for previous and future terms,the :order =\> date\_from  
is  
needed.

Thanks for your help

-ants

2009/10/30 Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> belongs\_to :swimming\_pool  
> I want to order my output as …
> 
> ```
> <li>Pool: <%= swimming_pool.name %>
> 
> ```
> 
> \<% end -%\>  
> If someone can just point me to the right place where I can read up on  
> you said “date\_from DESC”. Since it doesn’t make much sense to specify a  
> :conditions =\> [‘date\_from BETWEEN ? AND ?’, Date.today,  
> ActiveRecord::Association proxy), then your output becomes simpler. Even if  
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid
> 
> >

–  
100% naturally selected. 0% designed.
