【发布时间】:2013-03-07 11:19:20
【问题描述】:
在我的新 Rails 项目中,我需要访问旧数据库。所以我创建了一些遗留模型。 我在照片和 cmets 之间有一个多态关联(commentable_id 和 commentable_type)
当我打电话时
旧版::Photo.last.cmets
它不起作用,因为 commentable_type 是“Photo”而不是“LegcayPhoto”。
SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 [["commentable_id", 123], ["commentable_type", "Legacy::Photo"]]
legacy/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
legacy/comment.rb
module Legacy
class Comment < ActiveRecord::Base
establish_connection "legacy_#{Rails.env}"
#?? belongs_to :commentable, :polymorphic => true
end
end
我在 legacy/cmets.rb 中也有问题。 有没有办法为 belongs_to :commentable, :polymorphic => true 添加命名空间?
【问题讨论】:
-
在Comment模型中,应该是belongs_to :photo, :class_name => Legacy::Photo
-
但它不再是多态的。
标签: ruby-on-rails ruby-on-rails-3 rails-activerecord