【发布时间】:2012-02-25 04:12:34
【问题描述】:
我正在观看关于多态关联的 RailsCast。 http://railscasts.com/episodes/154-polymorphic-association?view=asciicast
共有三个不同的模型文章、照片和事件,每个模型都从 Comment.rb 中获取评论。 (文章、照片和事件中的每一个都有 article_id、photo_id 和 event_id)。在列出 cmets 时,他有一个问题是要找出列出 cmets 的 3 个模型中的哪一个,所以他在索引操作中这样做
def index
@commentable = find_commentable
@comments = @commentable.comments
end
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
我的问题是,$1 是什么?
【问题讨论】:
标签: ruby