开发者

Rails how to sort mulitple columns using where?

开发者 https://www.devze.com 2023-04-12 17:55 出处:网络
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.

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| %>

Rails how to sort mulitple columns using where?

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


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

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


This ought to do it:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号