新しいRailsプロジェクトでは、古いデータベースにアクセスする必要があります。そこで、いくつかのレガシーモデルを作成しました。写真とコメントの間にポリモーフィックな関連付けがあります(commentable_idとcommentable_type)
電話すると
Legacy :: Photo.last.comments
commentable_typeは「LegcayPhoto」ではなく「Photo」であるため、機能しません。
SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 [["commentable_id", 123], ["commentable_type", "Legacy::Photo"]]
レガシー/photo.rb
module Legacy
class Photo < ActiveRecord::Base
establish_connection "legacy_#{Rails.env}"
belongs_to :user, :class_name => 'Legacy::User' #works fine
has_many :comments, :class_name => 'Legacy::Comment', :as => :commentable
end
end
レガシー/comment.rb
module Legacy
class Comment < ActiveRecord::Base
establish_connection "legacy_#{Rails.env}"
#?? belongs_to :commentable, :polymorphic => true
end
end
また、legacy/comments.rbにも問題があります。:commentable、:polymorphic => trueのbelongs_toの名前空間を追加する方法はありますか?