2014-03-27 1 views
0

테이블이 Exchanges, CompanyCompany_Listings 인 데이터베이스가 있습니다.ROR 응용 프로그램 - 기본 범위 순서 변경 시도

Exchanges table 
    name 
    full_name 

Companies table 
    name 
    full_name 
    exchange_id 

Company_Listings table 
    company_id 
    exchange_id 
    company_code 
    option_name 
    [...] other columns 

업체 정보는 company_code입니다. BHP이고 회사 옵션 인 경우 option_name이됩니다. BHPXX는 아니지만 company_code은 없습니다. 두 회사 모두 company_id을 통해 "Broken Hill Billiton Pty Ltd."

Exchanges model 
    has_many :companies, dependent: :destroy 
    has_many :company_listings, dependent: :destroy 

Companies Model 
    belongs_to :exchange 
    has_many :company_listings, 

Company_Listing Model 
    belongs_to :company 
    belongs_to :exchange 
    default_scope { order('exchange_id ASC', 'company_code ASC', 'option_name ASC') } 

기본 범위는 회사 목록을 BHP, CBA ... BHPXX로 주문합니다.

내가 BHP, BHPXX, CBA를 제공하도록 정렬을 얻으려고합니다. 감사의 말을 전한 도움.

답변

0

이 시도 :

default_scope { order('company_code ASC', 'option_name ASC', 'exchange_id ASC')

를가 여기에 끝났다 어떻게 널 (null), 당신이 볼 필요가 수 있기 때문에 문제가 해결되지 않으면 :

ORDER BY ASC with Nulls at the Bottom

MySQL Orderby a number, Nulls last

default_scope { order('-company_code DESC', '-option_name DESC', 'exchange_id ASC')

EDIT : company_code 및 option_name이 본질적으로 숫자가 아니므로 위의 내용이 작동하지 않을 수 있지만 링크는이를 수행 할 수있는 많은 다른 방법을 제공합니다.

+0

아직 거기 있지만 당신은 대답을 내게 아이디어를 주셨습니다 - 감사합니다 – user1854802