Order by variable field name

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.

On 14 April 2010 12:29, ashu [email protected] 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

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.
[email protected]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

[email protected][email protected]
.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Thanks:
Rajeev sharma

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs