Dans mon nouveau projet Rails, j'ai besoin d'accéder à mon ancienne base de données. J'ai donc créé des modèles hérités. J'ai une assocation polymorphes entre les photos et les commentaires (commentable_id et commentable_type)has_many avec class_name dans les associations polymorphes
Quand j'appelle
héritage :: Photo.last.comments
il ne fonctionne pas parce que le commentable_type est 'Photo' et non 'LegcayPhoto'.
SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 [["commentable_id", 123], ["commentable_type", "Legacy::Photo"]]
héritage/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
héritage/comment.rb
module Legacy
class Comment < ActiveRecord::Base
establish_connection "legacy_#{Rails.env}"
#?? belongs_to :commentable, :polymorphic => true
end
end
J'ai aussi un problème dans l'héritage/comments.rb. Existe-t-il un moyen d'ajouter l'espace de noms pour belongs_to: commentable,: polymorphic => true?
Dans le modèle Comment, il devrait être belongs_to: photo,: class_name => Legacy :: Photo –
Mais alors ce n'est plus polymorphique. – mm1