2013-10-31 9 views
5

Sto cercando di aggiornare un progetto da Rails 3 Rails a 4. In Rails 3 stavo facendo:ordine sbagliato riguardante l'associazione has_many durante l'aggiornamento da rotaie 3 a 4 binari

class Sale < ActiveRecord::Base 
    has_many :windows, :dependent => :destroy 
    has_many :tint_codes, :through => :windows, :uniq => true, :order => 'code ASC' 
    has_many :tint_types, :through => :tint_codes, :uniq => true, :order => 'value ASC' 
end 

Quando chiamo sale.tint_types , lo fa la seguente query in Rails 3:

SELECT DISTINCT "tint_types".* FROM "tint_types" INNER JOIN "tint_codes" ON "tint_types"."id" = "tint_codes"."tint_type_id" INNER JOIN "windows" ON "tint_codes"."id" = "windows"."tint_code_id" WHERE "windows"."sale_id" = 2 ORDER BY value ASC 

ho aggiornato per Rails 4 in questo modo:

class Sale < ActiveRecord::Base 
    has_many :windows, :dependent => :destroy 
    has_many :tint_codes, -> { order('code').uniq }, :through => :windows 
    has_many :tint_types, -> { order('value').uniq }, :through => :tint_codes 
end 

Le modifiche della query a:

SELECT DISTINCT "tint_types".* FROM "tint_types" INNER JOIN "tint_codes" ON "tint_types"."id" = "tint_codes"."tint_type_id" INNER JOIN "windows" ON "tint_codes"."id" = "windows"."tint_code_id" WHERE "windows"."sale_id" = $1 ORDER BY value, code 

Si aggiunge codice nella clausola ordine e questo rende PostgreSQL a attraverso un errore. Presumo che sia a causa dell'ambito, ma non riesco a capire come ottenere quel codice ORDER BY.

Qualsiasi aiuto è gradito, Grazie!

+0

try 'unscoped.order ('value'). Uniq' – tihom

+0

Già provato. Non funziona Sembra che prima abbia impostato l'ordine da tint_types e dopo quello da tint_codes. – st3fan

risposta

4

La comunità Rails mi ha aiutato a trovare la soluzione.

class Sale < ActiveRecord::Base 
    has_many :windows, :dependent => :destroy 
    has_many :tint_codes, -> { order('code').uniq }, :through => :windows 
    has_many :tint_types, -> { uniq }, :through => :tint_codes 

    def tint_types 
    super.reorder(nil).order(:width => :asc) 
    end 
end 

Per ulteriori dettagli, vedere https://github.com/rails/rails/issues/12719.

1

Modificare il tint_types associazione

has_many :tint_types, -> { reorder('value').uniq }, :through => :tint_codes 
+0

Fa la stessa query di quando utilizzo solo l'ordine. – st3fan