我的控制器中有搜索方法:
def search
@search = Sunspot.search [Post, Siri] do
fulltext params[:q]
end
@posts = @search.results
end
然后我在我的观点中有这个:
- @posts.each do |p|
%h5= p.title
我的问题是如何在Post模型中分别显示@posts&如果来自Siri模型的@posts。
我个别想要的原因是因为Post模型中不存在某些attr但是在Siri模型中存在。
提前致谢!
答案 0 :(得分:2)
在班级你可以使用is_a吗?检查记录是否属于哪个类:
- if p.is_a? Post
// here will be from post model
- elsif p.is_a? Siri
// here will be from siri model
如果属于atrributes级别,您可以使用respond_to吗?来自ruby,当对象具有此属性时将返回true,否则将为false:
- if p.respond_to?(:title)
%h5=p.title