DataMapper的与条件ñ
-
22-09-2019 - |
题
以任何机会是有可能创建与DataMapper的条件关联?
例如:
我希望用户具有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
我知道,这是有可能通过创建从用户的子类的开发者::用户和在类中,使用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的容易执行快速和直接的方法:)
在预先感谢
解决方案
目前,条件是你在关系声明中只包括应用于目标。因此,如果目标模型有:活跃的房地产,你可以说这样的话has n, :apps, :active => true
。不幸的是你无法定义是只给源(还)的当前状态活性的关系。
有我正在考虑扩大在DM查询逻辑提出了一些建议,但我不能确定的影响将是代码什么,以及额外的功能,但是从本提供备用。这可能是我们DM 1.0后处理的东西,因为它也影响到50+适配器和插件。
STI通常是什么我建议这样的事情,因为它可以让你定义只存在该种对象的关系。另一种方法是定义为正常的关系,标记存取/ mutator方法为私有,然后添加不return apps if developer?
的等效代理方法。
不隶属于 StackOverflow