DataMapper 有 n 个带条件的
是否有可能与 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
我知道这可以通过从 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 轻松执行快速而直接的方法:)
提前致谢
By any chance is it possible to create a conditional association with DataMapper?
For example:
I want the User have n Apps just if that user have the attribute :developer => true
something like this:
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
I know that this would be possible by creating a subclass from User as a Developer::User and in that class, use the has n
, but I really would like to know if its possible to make it directly on the association declaration.
Another way I also managed to do when using ARn was to extend the association and rewriting the methods for each action.
So on the extension module I could have something like this:
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
But again, I really would like to avoid the use of this solutions if possible, but just if it's possible to perform a quick and direct method easily with DataMapper :)
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前,您在关系声明中包含的条件仅适用于目标。因此,如果目标模型具有 :active 属性,您可以说
has n, :apps, :active =>;正确。不幸的是,您无法定义仅在给定源当前状态的情况下才处于活动状态的关系(目前)。
我正在考虑一些建议来扩展 DM 中的查询逻辑,但我不确定这会对代码产生什么影响,以及除此之外它将提供哪些额外功能。这可能是我们在 DM 1.0 之后解决的问题,因为它还会影响 50 多个适配器和插件。
对于此类情况,我通常建议使用 STI,因为它允许您定义仅针对该类型对象存在的关系。另一种方法是将关系定义为正常关系,将访问器/修改器方法标记为私有,然后添加一个代理方法,该方法的作用相当于
return apps if Developer?
。At the moment, conditions that you include in the relationship declaration only apply to the target. So if the target model has an :active property, you can say things like
has n, :apps, :active => true
. Unfortunately you can't define relationships that are only active given the current state of the source (yet).There are some proposals I'm considering to expand the Query logic in DM, but I'm unsure what the impact will be to the code, and what extra capabilities it will provide aside from this. It may be something we tackle after DM 1.0, since it also affects 50+ adapters and plugins.
STI is normally what I'd recommend for something like this, since it will allow you to define relationships that only exist for that type of object. Another approach would be to define the relationships as normal, mark the accessor/mutator methods as private, and then add a proxy method that does the equivalent of
return apps if developer?
.