2012-04-10 22 views
5

के लिए वर्ग के नाम को परिभाषित यह मेरे पुराने डेटाबेस से डेटा ले जाने के लिए मेरी कोड है एसक्यूएलरूबी, बहुरूपी रिश्ते

SELECT `uploaded_files`.* FROM `uploaded_files` WHERE `uploaded_files`.`storage_id` = 38 AND `uploaded_files`.`storage_type` = 'Old::Recipe' 

समस्या यह है कि मैं मिलता है:

`storage_type` = 'Old::Recipe' 

लेकिन मैं की जरूरत है:

`storage_type` = 'Recipe' 

मैं कैसे एक बहुरूपी रिश्ते के लिए वर्ग बदल सकता हूँ?

has_many के लिए दस्तावेज़ मुझे कोई जवाब नहीं देता है।

+0

क्या कोई कारण है कि पकाने की विधि पुरानी कक्षा के अंदर घोंसला है? – pixeltrix

+0

शायद यह एक रेल इंजन @ पिक्सेलट्रिक्स में स्थानांतरित हो गया था। किसी के पास इसके लिए बेहतर जवाब है? – Jwan622

उत्तर

0

दुर्भाग्य से, अब के लिए मैं सिर्फ एक ही रास्ता है कि ऐसा करने के लिए मिला:

class Old < ActiveRecord::Base 
    establish_connection :old_version 
    self.abstract_class = true 

    class Recipe < self 
    set_table_name :recipes 
    has_many :old_files, 
      :class_name => 'UploadedFile', 
      :finder_sql => Proc.new { 
       %Q{ 
        SELECT uploaded_files.* 
        FROM uploaded_files 
        WHERE uploaded_files.storage_id = #{id} AND 
         uploaded_files.storage_type = 'Recipe' 
       } 
       } 
    end 

    class UploadedFile < self 
    set_table_name :uploaded_files 
    belongs_to :storage, :polymorphic => true 
    end 
end 


namespace :old do 
    task :menu => :environment do 
    Old::Recipe.all.each do |recipe| 
     puts '~' * 50 
     puts recipe.id 
     recipe.old_files.to_a.each do |file| 
     puts file.storage_id, file.storage_type 
     end 
    end 
    end 
end 

बहुत बहुत दुख की बात

+0

क्या उपरोक्त टिप्पणी काम नहीं किया? – Jwan622

1

हाल ही में मैं, यह एक समाधान है कि रेल 4.2 में मेरे लिए काम किया है इसी तरह की समस्या थी:

class Recipe < self 
    set_table_name :recipes 
    has_many :old_files, -> (object) { unscope(where: :storage_type).where(storage_type: 'Recipe') }, class_name: 'UploadedFile' 
end 

आपको क्वेरी से uploaded_files.storage_type = 'Old::Recipe' स्थिति को हटाने के लिए unscope(:where) जोड़ना होगा।

+0

यह काम करता है? – Jwan622