我正在努力实现多态注释(这些注释可以应用于网站上的任何用户内容,并且不仅限于Article
个实例。在创建注释时,我需要确定哪个{{1我在这个主题上发现的大部分文字表明我在下面的代码中使用了commentable
中指定的模式,但这种方法并没有让我觉得非常优雅 - 看起来应该是这样的是一种直接的方法,可以明确地指定find_commentable
正在创建新注释,而不需要遍历commentable
集,也不需要字符串匹配。有更好的方法吗?
换句话说,是否有更好的方法可以在params
→commentable
关联的上下文中从comment
控制器访问commentable
对象?它是否仍然适用于我们尚未使用comment
对象的create
方法?
我的模型设置如下:
@comment
谢谢!
答案 0 :(得分:9)
我也在寻找答案,并希望分享Launch Academy's Article on Polymorphic Associations,因为我觉得它提供了最简洁的解释。
对于您的应用程序,还有两个选项:
<强> 1。 &#34; Ryan Bates&#34;方法:(当您使用Rails&#39;传统RESTful URL时)
def find_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
end
<强> 2。嵌套资源:
def find_commentable
commentable = []
params.each do |name, value|
if name =~ /(.+)_id$/
commentable.push($1.classify.constantize.find(value))
end
end
return commentable[0], commentable[1] if commentable.length > 1
return commentable[0], nil if commentable.length == 1
nil
end
第3。单一资源:(您的实施,但重复完成)
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
答案 1 :(得分:5)
我会反过来做 - 然后让评论定义可评论。
@comment = Comment.create(params[:comment]) #this is the standard controller code for create
@commentable = @comment.commentable
答案 2 :(得分:1)
我接受了Ryan Bates的想法,并稍微调整了一下。我有一些更深入的作用域和嵌套资源。
id, resource = request.path.split('/').reverse[1,2]
@commentable = resource.singularize.classify.constantize.friendly.find(id)
这里的想法是你从路径末端剥离父母。
因此,如果你的路径是/scope/resource_a/1234/resource_b/5678/comments
等,无论你有多深,你总是得到顶级父母。