【发布时间】:2010-10-23 06:31:32
【问题描述】:
我有一个简单的多态关联
#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.cmets 或 Comment.cmets。
但是我怎样才能找到父帖子?
就像在 Comment.post 中一样?
我目前可以通过执行一系列.commentable 来获得它们。例如:
Comment.find(1).commentable.commentable
=> Post(:id => ...
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 polymorphic-associations arel