0

I am kind of new at rails, currently using version 3.23 and I am trying to enable 'sort' on two columns in my table. I managed to create the links on these column headers and actually got one column working/sorting!But couldnt achieve the same result when I modified my code in the movie controller rb, my code can only work for one column! is:

def index
  @movies = Movie.all.sort_by { |movie| movie.title } 
end

Works perfectly, but when I combine another parameter i.e. release date I get an error!

def index
  @movies = Movie.all.sort_by { |movie| movie.title } then { |release date| release.date}
end

Can someone please help me resolve this issue? I have researched it on google but I've gotten nothing conclusive!. Any help will be most appreciated.

1
  • [movie.title, movie.release.date] or whatever release actually is. You're not "combining" anything, you're using random syntactic expressions instead of searching the web for sort_by multiple fields, which turns up things like stackoverflow.com/questions/17076372/…. Arguably you'd be better served by sorting on the DB side instead of getting all the movies and sorting in Ruby, though. Commented Apr 1, 2014 at 18:32

2 Answers 2

0

Assuming that you have to sort on title and release_date fields in movies table.

You can perform the sorting at database level itself as below:

In Rails 4.x:

Below will sort all the movie records with title and release_date in ascending order(as default).

def index
  @movies = Movie.order(:title, :release_date)
end

If you want to change the order, you can specify as asc or desc as:

def index
  @movies = Movie.order(title: :asc, release_date: :desc)
end

In Rails 3.x:

def index
  @movies = Movie.all(:order => "title ASC, release_date ASC")
end

If you want to change the order, you can specify as DESC in the above case.

Sign up to request clarification or add additional context in comments.

6 Comments

@user3435842 Glad to help :). Please accept the answer when you get a chance. Also, was I correct on release_date as a field and not release.date?
Kirti you were indeed correct on release_date,though I would like my columns to sort both directions :asc and :desc upon clicking on column! I am trying to figure out a way of making them more dynamic
Glad to help :). I suppose you are looking for tablesorter kinda thing. In that case Javascript would come in picture. Take a look at jquery-tablesorter gem. It might help. Also, I would recommend you to try to resolve the problem on your own first. When you get stuck you could always post a question, so some expert can guide you in proper direction.
Kirti thank you so much for your assistance.. I think i figured it out.
Thats great!! Good job :)
|
0

You can order your listing by this

def index
    @movies = Movie.order(title: :asc, release_data: :desc)
end

1 Comment

Thank you so much Kirti Thorat and Mayank, those methods worked perfectly!! I truly appreciate your time in assisting me, pity I couldn’t figure it out on my own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.