Rails beginner
Rails beginner

Reputation: 14514

Rails how to sort mulitple columns using where?

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.

Upvotes: 2

Views: 783

Answers (3)

Mailo Světel
Mailo Světel

Reputation: 26060

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

Upvotes: 1

emrass
emrass

Reputation: 6444

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

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

Upvotes: 4

Jordan Running
Jordan Running

Reputation: 106147

This ought to do it:

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

Upvotes: 1

Related Questions