2

How do I order an instance variable using two columns?

I want to order after reel_order and reel_online which as a boolean and should be true.

I have tried this:

<% @movies.find(:all, :order => "reel_order, where(reel_online) = 1").each do |movie| %>

Is it possiable what activerecord or how to order this when using MySQL as database. I also want to know how to do it with a postgreSQL database.

UPDATE:

I am using this in my loop:

<% @movies.find(:all, :order => "reel_order, reel_online DESC").each do |movie| %>

enter image description here

The result is that the @movie is not ordered after reel_online. The reel_online is true when the eye is open. I want the open eyes to be at the top as expected.

3
  • You want to sort by reel_order with a secondary sort on the boolean result of the reel_online = 1 expression? Commented Oct 14, 2011 at 0:44
  • Yes :) That is exactly what I want to do. Commented Oct 14, 2011 at 0:59
  • 1
    I wrote whole answer before i noticed. You want to sort by reel_online and choose only records where reel_online is TRUE? It make no sense to me. Sorting make sense only when you have more than one possible values. When you limit to those which are true, you have only one possible value of that column Commented Oct 14, 2011 at 12:07

3 Answers 3

4

I think you mixed the order of the columns. It should be

<% @movies.find(:all, :order => "reel_online DESC, reel_order").each do |movie| %>
Sign up to request clarification or add additional context in comments.

2 Comments

the first column gets ordered last ?
No, it gets ordered first. So with your previous statement, you order by reel_order first, and among these items by reel_online. If you have 5 movies with reel_order = 1, those with reel_online will come first. If you have another movie with reel_order = 2 and reel_online = 1, this will come afterwards. You want it the other way round: Order by reel_online (putting those with reel_online = 1 first) and as second order criteria by reel_order.
1

This ought to do it:

@movies.order("reel_order, reel_online DESC")

1 Comment

@Jordan show us the relevant query being generated from the console output, please.
1

Igor Kapkov has similar aproach as me, but would me more active active record

@movies = Movies.find_all_by_reel_online(  1,
                                           :order => 'reel_order ASC, reel_online ASC'
                                           :conditions => ['reel_online = TRUE']
)

Documentation of find method http://apidock.com/rails/ActiveRecord/Base/find/class

Few database searches of mine https://github.com/roolo/mwstt/blob/master/app/controllers/datetimes_controller.rb

BTW: The <% leads me you are selecting the records in view. You should not put it in here

Comments

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.