我正在構建的應用程序的一部分是API。最近的變化意味着我需要將兩個不同版本的數據放到我的json feed中。我認爲最好的辦法是在數據庫中進行必要的更改,然後創建一個虛擬屬性來連接數據。在選擇查詢中返回虛擬屬性
在我的模型我有沒有問題,使用<%= @event.event_summary =>
在視圖中輸出EVENT_SUMMARY虛擬屬性:
def event_summary
"#{title} (#{start_datetime.strftime('%A %d %b, %l:%M%P')})"
end
在我的API控制器我有一個選擇查詢,它得到我需要的API響應屬性(這裏的簡化版本):
respond_to :json
def index
respond_with Event.select('title, event_summary')
end
的問題是,它總是會返回一個未定義的列錯誤:
PG::UndefinedColumn: ERROR: column "event_summary" does not exist LINE 1: SELECT title, event_summary FROM "events"
我也試過以下,但得到了同樣的結果:
respond_with Event.select('title', :event_summary)
我使用Postgres的,如果有什麼差別。
有沒有辦法做到這一點?
謝謝!
這正是我所期待的。謝謝! – samlester