有沒有可能使用DataMapper創建一個條件關聯?DataMapper有n條件
例如:
我希望用戶有n個應用程序只是如果用戶有屬性:developer => true
是這樣的:
class User
include DataMapper::Resource
property :id, Serial
property :name, String, :nullable => false
property :screen_name, String, :nullable => false, :unique => true
property :email, String, :nullable => false, :unique => true, :format => :email_address
property :password, BCryptHash, :nullable => false
property :developer, Boolean, :default => false
#The user just gets apps if developer
has n :apps #,:conditions => "developer = 't'"
end
class App
include DataMapper::Resource
property :id, Serial
property :name, String, :nullable => false
belongs_to :user
end
我知道,這將有可能通過創建User作爲Developer :: User的子類,並且在該類中使用has n
,但我真的很想知道它是否可以直接在關聯聲明上生成它。
當我使用ARn時,另一種方法是擴展關聯並重寫每個動作的方法。
所以擴展模塊上我能有這樣的事情:
module PreventDeveloperActions
def new
if proxy_owner.developer?
super
else
raise NoMethodError, "Only Developers can create new applications"
end
end
# and so on for all the actions ...
end
但同樣,我真的想避免使用此解決方案,如果有可能的,只是如果有可能執行快速和容易與DataMapper的:)
由於直接的方法提前
嗨丹,非常感謝你的關注和支持,它非常感謝:)我會嘗試你提到的這些方法,但我很確定這會解決我的問題:)再次感謝 – zanona 2009-11-30 13:04:39