我有一個用於資產管理的API,並且我使用Her構建了API客戶端的Rails應用程序,但也處理身份驗證。如何關聯她和Active Record模型?
我有一個用戶活動記錄模型。我如何將她的模型和Active Record模型聯繫起來?
的代碼是這樣的:
class Resource
include Her::Model
has_many :bookings
parse_root_in_json true, format: :active_model_serializers
end
class Booking
include Her::Model
belongs_to :User
belongs_to :Resource
parse_root_in_json true, format: :active_model_serializers
end
class User < ActiveRecord::Base
has_many :Bookings
end
目前,如果我試圖讓用戶的預訂,我獲得存儲在預約對象id字段,不是我需要的用戶對象。
irb(main):067:0* Resource.find(1).bookings.where(status: 'all').first.user
=> "1"
對於其他讀者,誰不知道她的是什麼:'她'是古老的'ActiveResource'的替代品。鏈接:https://github.com/remiprev/her我以前從來沒有聽說過它,似乎是一個偉大的寶石。這將有助於解釋這個問題多一點,我想這導致了downvote?故意在'belongs_to'中是否大寫'User'和'Resource'? – nathanvda 2014-12-02 14:24:29