2010-11-03 62 views
4

我有一個基於ActiveRecord的模型,另一個基於Mogoid :: Document。有可能一起做一個協會嗎?ActiveRecord和Mogoid :: Document:關聯

例如,2種型號:

class User < ActiveRecord::Base 
    has_one :avatar, :dependent => :destroy 
end 

class Avatar 
    include Mongoid::Document 
    field :file_name 
end 

並獲取用戶的頭像是這樣的:

@user.avatar.file_name 

謝謝!

回答

10

您將無法使用ActiveRecord關係。

你仍然可以使用鏈接實例方法的兩個對象是這樣的:

class User < ActiveRecord::Base 

    def avatar 
    Avatar.where(:user_id => self.id).first 
    end 

    def avatar=(avatar) 
    avatar.update_attributes(:user_id => self.id) 
    end 

end 

這將是有趣的模塊中封裝的:)...

+0

非常感謝。你的代碼和平運作良好! – Tibal 2010-11-09 11:01:13

+0

你可以使用Tenacity(https://github.com/jwood/tenacity)來代替黑客自己的解決方案。 – Raphael 2012-07-10 18:25:34

-2

不,這是不可能的。 ActiveRecord期待關聯是一個AR對象。你曾經可以將Mongoid與AR相關聯,但現在它也不能工作。

+0

這個答案是錯誤的。 – Raphael 2012-10-03 21:28:35