2013-09-10 53 views
1

MorningPostgresql訂單模型與Rails中的關係布爾型字段

我不知道如何讓pg按關係布爾字段排序。真實是最重要的。

這是我的解決方案,但它太重,因爲使用sort_by。可以用訂單覆蓋嗎?

Solution.order(sort_column + ' ' + sort_direction).sort_by{|a| a.user.admin? || a.user.moderator? ? 1: 0} 

class Solution < ActiveRecord::Base 
    include Voteable, Classifiable, Archiveable, GeoTaggable 
    include WithAssets, WithStats, WithEvents 

    belongs_to :user 
end 

方案

CREATE TABLE users (
    id integer NOT NULL, 
    ... 
    admin boolean DEFAULT false, 
    moderator boolean 
); 

CREATE TABLE solutions (
    id integer NOT NULL, 
    user_id integer, 
    body text, 
    ... 
); 

回答

1

順序數據庫工作。您應該添加更多關於您的數據庫模式的信息或使用當前的解