我在 ruby on rails 上,我有两个模型,目标是在两个模型上搜索网站,我正在使用 twitter typeahead 但我遇到的问题是 json 必须是一个对象。
我不确定将两个对象合二为一的最佳方法是什么。这里的代码
@users= Search.user(params[:query])
@articles= Search.article(params[:query])
respond_to do |format|
format.html # index.html.erb
format.json {
render :json => {
:art=> @articles.map(&:title),
:user=> @users.map(&:first_name)
}}
end
end
我不确定最好的方法是什么,或者我似乎找不到将这两个模型合并为一个的最佳文档。我不知道 to_json、as_json 或 concat 是否是最好的。
这个想法是从以下 json 得到结果
{"art":["John","Serge","Dean","feng","Heather"],"user":["Ontario high school teachers drop next week's walkout plan","Air Canada to appeal Quebec court ruling on Aveos"]}
到以下
{"result":["John","Serge","Dean","feng","Heather", "Ontario high school teachers drop next week's walkout plan","Air Canada to appeal Quebec court ruling on Aveos"]}