Я просмотрел похожие посты, но, похоже, не совсем понял.
У меня есть следующая функция, которая прекрасно работает. Модель Listing имеет внешний ключ с именем price_id , который сопоставляется с моделью Price и столбцом price_range . Price_id возвращается как часть объекта message в ответе JSON.
Как можно вернуть соответствующее значение price_range из ассоциации вместо значения price_id (как часть сообщения obj, и сохранить другие атрибуты )
def update
@listing = Listing.find(params[:listing][:id])
#if params were passed in for updating
if @listing.update_attributes(params[:listing])
#should we return the whole thing or just what's needed?
json_response = {
"success" => @listing.save, #save to DB and assign true/false based on success...
"message" => @listing.attributes #USE attributes to show output the content of the @message obj, and not another object called "message"
}
respond_to do |format|
#json response
format.html { render:json => json_response }
format.xml { render :xml => @listing }
#normal response. Consider leaving this for now?
#format.html { render :action => "detail" } #refresh this page, with new data in it. Consider trying to use redirect instead?
#format.xml { head :ok }
end
end #end if
end