数据映射器 =>一对多过滤器
class Task
include DataMapper::Resource
has 1, :list, :through => Resource
end
class List
include DataMapper::Resource
has n, :tasks, :through => Resource
end
一个列表有很多任务。假设我有一个 id = 1 的任务。
如何找到包含此任务的列表?
我尝试过:List.first(:tasks => task) 但它总是返回nil。
谢谢。
class Task
include DataMapper::Resource
has 1, :list, :through => Resource
end
class List
include DataMapper::Resource
has n, :tasks, :through => Resource
end
A list has many tasks. Suppose I have a task with id = 1.
How do I do to find the list that has this task?
I tried : List.first(:tasks => task) but it always returns nil.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Task.first(:id => 1).list
怎么样?顺便说一句,您确实应该更改模型的定义。我建议您彻底阅读datamapper文档。这样是不是看起来好看多了?哦,我希望你定义了键。这些对于运作良好的协会很重要。如果
id
是Task
的键,您的查询将简化为Task.get(1).list
。what about
Task.first(:id => 1).list
? By the way, you should really change the definitions of your models. I recommend you to read the datamapper documentation thoroughly.Doesn't that look much nicer? Oh and I hope you defined keys. These are important for well working associations. And if
id
is the key forTask
your query would simplify toTask.get(1).list
.您可以使用这样的嵌套条件:
但给定一个任务,使用
task.list
会更简单You can used a nested condition like this:
but given a task it would be simpler to use
task.list
为什么你有1通过资源?我会先执行 Task.belongs_to :list 然后执行 List.has n, :tasks 这样你就可以写:
虽然如果你已经得到了任务,那么写 task.list 就更简单了:)
Why do you have has 1 through resource? I would do Task.belongs_to :list and then List.has n, :tasks so you could write:
Although if you already got the task then it's simpler to just write task.list :)