我有一个简单的多态关联
#comment.rb
belongs_to :commentable, :polymorphic => true
has_many :comments, :as => :commentable
#post.rb
has_many :comments, :as => :commentable
accepts_nested_attributes_for :comments, :allow_destroy => true
所以在 IRB 中我可以做 Post.comments 或 Comment.comments。
但是我怎样才能找到父帖子?
就像在 Comment.post 中一样?
我目前可以通过执行一系列.commentable
's 来获得它们。例如 :
Comment.find(1).commentable.commentable
=> Post(:id => ...