# Is there any better way to do this?

**URL:** <https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589>\
**Category:** Rails\
**Created:** [January 25, 2009, 3:07pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589 "2009-01-25T15:07:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 25, 2009, 3:07pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589/1 "2009-01-25T15:07:51Z")

</div>

#order can be “date” or “score” or “user”

@order = “date” #default  
@order = session[:order] if session[:order]  
@order = params[:order] if params[:order]

orderby = “updated\_on DESC” #default  
orderby = “score DESC” if @order == “score”  
orderby = “user” if @order == “user”

session[:order] = @order

@results = Stats.find(:all, :order =\> orderby, :conditions…

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 25, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589/2 "2009-01-25T17:13:19Z")

</div>

- James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-01-25 15:07:51  
+0100]:

> session[:order] = @order
> 
> @results = Stats.find(:all, :order =\> orderby, :conditions…

@order = params[:order] || session[:order] || ‘date’  
orderby = case @order  
when ‘user’ then ‘user’  
when ‘score’ then ‘score DESC’  
else ‘updated\_on DESC’  
end

my $0.02  
Jan

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 25, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589/3 "2009-01-25T17:20:57Z")

</div>

James B. wrote:

> #order can be “date” or “score” or “user”

Don’t use @ unless you are really passing a variable to other methods in  
this class.

> @order = “date” #default  
> @order = session[:order] if session[:order]  
> @order = params[:order] if params[:order]

order = session[:order] || params[:order] || ‘date’

Note I use single ‘ticks’ because I don’t need the special abilities of  
“”. That  
represents a very important style rule - use the simplest code you can.  
Think of  
“” as “costing more” than ‘’.

> orderby = “updated\_on DESC” #default  
> orderby = “score DESC” if @order == “score”  
> orderby = “user” if @order == “user”

orderby = order == ‘date’ ? ‘updated\_on’ : order

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 25, 2009, 5:40pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589/4 "2009-01-25T17:40:31Z")

</div>

- Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-01-25 08:32:22 -0800]:

> But why the params and session themselves don’t contain the real code -  
> ‘updated\_on DESC’. The View could, for example, show ‘date’ to the user and set  
> its value to ‘updated\_on DESC’. Then all this fun goes away!

Agree. The only reason may be security - user would know your table  
column name ‘updated\_on’ if you use it directly in view.

Jan

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 25, 2009, 5:33pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589/5 "2009-01-25T17:33:23Z")

</div>

Xie H. wrote:

> orderby = case @order  
> when ‘user’ then ‘user’  
> when ‘score’ then ‘score DESC’  
> else ‘updated\_on DESC’  
> end

That’s better than mine by preserving the DESC.

But why the params and session themselves don’t contain the real code -  
‘updated\_on DESC’. The View could, for example, show ‘date’ to the user  
and set  
its value to ‘updated\_on DESC’. Then all this fun goes away!

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 25, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/is-there-any-better-way-to-do-this/158589/6 "2009-01-25T17:59:23Z")

</div>

Xie H. wrote:

> > But why the params and session themselves don’t contain the real code -  
> > ‘updated\_on DESC’. The View could, for example, show ‘date’ to the user and set  
> > its value to ‘updated\_on DESC’. Then all this fun goes away!
> 
> Agree. The only reason may be security - user would know your table  
> column name ‘updated\_on’ if you use it directly in view.

They also might hack the params and put in a SQL injection attack.
