2012-03-14 142 views
0

下面的代碼在Rails 3中工作得很好,但它拋出一個錯誤不明確的列名錯誤:升級時到Rails 3.1

ActiveRecord::StatementInvalid: SQLite3::SQLException: ambiguous column name: 
id: SELECT "categories".id FROM "categories" 
INNER JOIN "categorizations" 
ON "categories"."id" = "categorizations"."category_id" 
WHERE "categorizations"."reason_id" = 283 ORDER BY id 

的問題似乎是在調用reason.category_ids

- reasons.each do |reason| 
    - cat_ids = reason.category_ids.map {|id| "cat_id_#{id}"}.join(" ") 
    %li{"data-reason-id" => reason.id, :class => cat_ids} 
    %a= reason.text 

class Reason < ActiveRecord::Base 
    has_many :categorizations 
    has_many :categories, :through => :categorizations 

更新: 當註釋掉這裏的最後一行時,問題就解決了。這是一個錯誤?

class Category < ActiveRecord::Base 
    has_many :categorizations 
    has_many :reasons, :through => :categorizations 

    scope :active, where("active = ?", true) 
    default_scope :order => 'id' 
+1

你在哪裏做排序? 'ORDER BY id'是模糊的部分,因爲'categories'和'categorizations'都有一個id列,我假設。 – ScottJShea 2012-03-14 19:19:48

+0

我在視圖中這樣做。 – Gady 2012-03-14 19:35:19

+0

它似乎是問題 class Category 'id' 我該如何解決它? – Gady 2012-03-14 19:35:30

回答

4

Here we go ...

[In Rails 3.1] Supplying options hash to with_scope, with_exclusive_scope and default_scope has also been deprecated: 

default_scope :order => "id DESC" 

Try

default_scope order('categories.id') 
相關問題