2011-07-25 72 views
0

鑑於這種模式在遺留代碼,以回報率2.3.11:with_scope提高「的MySQL ::錯誤未知欄」錯誤回報率2.3.11

class Assignment < ActiveRecord::Base 
    belongs_to :source_person 
    belongs_to :destination_person 
    belongs_to :laptop 

    def self.setScope(places_ids) 
    find_include = [:laptop => {:owner => {:performs => {:place => :ancestor_dependencies}}}] 
    find_conditions = ["place_dependencies.ancestor_id = (?)", places_ids] 

    scope = { :find => { :conditions => find_conditions, :include => find_include } } 
    Assignment.with_scope(scope) do 
     yield 
    end 
    end 
end 

每當到達Assignment.with_scope聲明,我得到以下例外:

Assignment Delete all (0.0ms) Mysql::Error: Unknown column 'place_dependencies.ancestor_id' in 'where clause': DELETE FROM `assignments` WHERE (`id` IN (17)) AND (place_dependencies.ancestor_id = (1)) 

我的模式是這樣的:

create_table "assignments", :force => true do |t| 
    t.date "created_at" 
    t.date "date_assigned" 
    t.time "time_assigned" 
    t.integer "source_person_id" 
    t.integer "destination_person_id" 
    t.integer "laptop_id" 
    t.text "comment" 
    end 

    add_index "assignments", ["destination_person_id"], :name => "assignments_destination_person_id_fk" 
    add_index "assignments", ["laptop_id"], :name => "assignments_laptop_id_fk" 
    add_index "assignments", ["source_person_id"], :name => "assignments_source_person_id_fk 

    create_table "place_dependencies", :force => true do |t| 
    t.integer "descendant_id" 
    t.integer "ancestor_id" 
    end 

    add_index "place_dependencies", ["ancestor_id"], :name => "place_dependencies_ancestor_id_fk" 
    add_index "place_dependencies", ["descendant_id"], :name => "place_dependencies_descendant_id_fk" 

我幾乎在同樣的代碼多種型號,但一切正常。你能給我一個提示,說明如何解決這個問題嗎?

回答

0

後做下面的工作原理:

def self.setScope(places_ids) 
    find_include = { 
     :laptop => { 
     :owner => { 
      :performs => { 
      :place => :ancestor_dependencies} 
     } 
     } 
    } 
    find_conditions = {"place_dependencies.ancestor_id" => places_ids} 

    Assignment.all :joins => find_include, :conditions => find_conditions 
    yield 
    end 

這個答案來自一個更有經驗的Ruby on Rails的開發者需要審查。