我有一個數據模型,其中User
可以像Project, Suggestion, Comment
或其他對象。 如果我們只是爲了支持Project
孩子渲染具有多態模型的RABL部分
object @like
attributes :id, :created_at, :target_type, :target_id, :user_id
child :user => :user do
extends "users/show"
end
child :target do
node do |project|
partial('projects/show', :object => project)
end
end
不過,我希望能夠用以及因target_type
的suggestions/show, comments/show
諧音的模型設置正確,並likes/show.rabl
作品。
我試過,但它不工作:
child :target do |u|
u.target.map do |r|
if r.is_a?(Suggestion)
partial("suggestions/show", :object => r)
elsif r.is_a?(Project)
partial("projects/show", :object => r)
end
end
end
我得到undefined method target for <Rabl::Engine:0x69fb988>
。但是,在第一種情況下,我沒有看到這個錯誤。有任何想法嗎?