# Order by variable field name

**URL:** <https://www.ruby-forum.com/t/order-by-variable-field-name/186926>\
**Category:** Rails\
**Created:** [April 14, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/order-by-variable-field-name/186926 "2010-04-14T13:32:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4bbf92/32.png) [@ashu](https://www.ruby-forum.com/u/ashu)\
**Post date:** [April 14, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/order-by-variable-field-name/186926/1 "2010-04-14T13:32:42Z")

</div>

What i have

Age  
name  
Sex  
what i did  
\<%= link\_to ‘Age’,:order\_by =\>  
‘age’, :action =\> ‘index’%\>  
\<%= link\_to ‘name’,:order\_by =\>  
‘name’, :action =\> ‘index’%\>  
\<%= link\_to ‘Sex’,:order\_by =\>  
‘Sex’, :action =\> ‘index’%\>  
What i need  
when user Click On Age or name or sex records  
should be in that order as user clicked.if user clicked on age Twice  
Then record should reverse . that would be bonus .  
By default it Should Be As working now.

---

<div class="post-metadata">

**Author:** ![ashu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4bbf92/32.png) [@ashu](https://www.ruby-forum.com/u/ashu)\
**Post date:** [April 14, 2010, 1:37pm UTC](https://www.ruby-forum.com/t/order-by-variable-field-name/186926/2 "2010-04-14T13:37:23Z")

</div>

On 14 April 2010 12:29, ashu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What i have

> What i need  
> when user Click On Age or name or sex records  
> should be in that order as user clicked.if user clicked on age Twice  
> Then record should reverse . that would be bonus .  
> By default it Should Be As working now.

OK, so given that this isn’t a free-coding forum or a homework-for-you  
forum, what do you need? Hints on how to do this? Do you have code  
you’ve  
tried that isn’t working?

I’ll start you with hints and you can then try it yourself…

1. The :order\_by in your link\_to will come through in the params hash in  
your controller as params[:order\_by]

2. You can use :order as an option to ActiveRecord find methods (along  
with  
“ASC” or “DESC” for ordering)

3. You’ll need to remember the current order if you want to toggle it on  
clicks (or have it as another parameter in your link\_to), you can use  
the  
session[:whatever\_you\_want] = … construct for that.

People are always happy to help, but you can’t just say “I need…”

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![ashu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4bbf92/32.png) [@ashu](https://www.ruby-forum.com/u/ashu)\
**Post date:** [April 14, 2010, 1:54pm UTC](https://www.ruby-forum.com/t/order-by-variable-field-name/186926/3 "2010-04-14T13:54:57Z")

</div>

Thanks Andy

I did this A Lot.

:order =\> “#{params[:order\_by]} = 1”

there is a Write Query Running in my logger But Nothing happens .i  
thinks  
there must be a previous Code Which making my code Disable.Leave I have  
to  
consult my Senior .  
any Ways thanks Again For the advice Which are More important for me  
About  
Manners  
Thanks A lot.

On Wed, Apr 14, 2010 at 5:05 PM, Andy J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > should be in that order as user clicked.if user clicked on age Twice
> 
> 1. The :order\_by in your link\_to will come through in the params hash in
> 
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Thanks:  
Rajeev sharma

---

<div class="post-metadata">

**Author:** ![ashu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4bbf92/32.png) [@ashu](https://www.ruby-forum.com/u/ashu)\
**Post date:** [April 14, 2010, 2:20pm UTC](https://www.ruby-forum.com/t/order-by-variable-field-name/186926/4 "2010-04-14T14:20:42Z")

</div>

> I did this A Lot.
> 
> :order =\> “#{params[:order\_by]} = 1”

This should probably be something more like:

session[:order\_direction] ||= “ASC”

:order =\> “#{params[:order\_by]} #{session[:order\_direction]}”

there is a Write Query Running in my logger But Nothing happens .i  
thinks

> there must be a previous Code Which making my code Disable.

OK

> Leave I have to consult my Senior .  
> any Ways thanks Again For the advice Which are More important for me About  
> Manners  
> Thanks A lot.

I assume this is sarcasm, but it’s really hard to tell.

Anyway, as I said I’m happy to help, but I need to know where you’re  
at/what  
the problem is (not just the requirement) and also need to feel valued  
that  
you’re not just using me/others here for free coding.

Cheers,

Andy
