比方說,我有:傳遞對象多態查詢參數的Rails查找/那裏
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Photo < ActiveRecord::Base
has_many :comments, :as => :commentable
#...
end
現在我想找到吉姆的照片所有評論:
@jims_photo = Photo.where(:of => "Jim")
@photo_comments = Comment.where(:commentable => @jims_photo)
這似乎是行不通的導軌(導軌3)。生成的查詢似乎無法展開多態對象爲commentable_id和commentable_type領域:
SQLite3::SQLException: no such column: comments.commentable:
我是新來的Ruby和Rails,所以我可能不正確使用的範例,但我的期望是,自動導軌擴展
:commentable => @jims_photo
到:
:commentable_id => @jims_photo.id, :commentable_type => @jims_photo.class.name
@ jims_photo.comments不起作用? – Thilo