Đây là mã của tôi cho việc di chuyển dữ liệu từ cơ sở dữ liệu cũ của tôi:Ruby on Rails, has_many, xác định tên lớp cho mối quan hệ đa hình
class Old < ActiveRecord::Base
establish_connection :old_version
self.abstract_class = true
class Recipe < self
set_table_name :recipes
has_many :uploaded_files, :as => :storage
end
class UploadedFile < self
set_table_name :uploaded_files
belongs_to :storage, :polymorphic => true
end
end
Khi tôi chạy đoạn mã sau
Old::Recipe.all.each do |recipe|
puts recipe.uploaded_files.to_sql
end
Nó thực hiện này SQL
SELECT `uploaded_files`.* FROM `uploaded_files` WHERE `uploaded_files`.`storage_id` = 38 AND `uploaded_files`.`storage_type` = 'Old::Recipe'
Vấn đề là tôi nhận được:
`storage_type` = 'Old::Recipe'
Nhưng tôi cần:
`storage_type` = 'Recipe'
Làm thế nào tôi có thể thay đổi lớp cho một mối quan hệ đa hình?
Tài liệu dành cho has_many
không cho tôi câu trả lời.
có lý do tại sao lớp Công thức được lồng trong lớp Cũ không? – pixeltrix
Có lẽ điều này đã được chuyển vào một Rails Engine @pixeltrix. Bất cứ ai có một câu trả lời tốt hơn cho điều này? – Jwan622