数据映射器 =>一对多过滤器

发布于 2024-10-12 04:33:32 字数 323 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心安伴我暖 2024-10-19 04:33:32

Task.first(:id => 1).list 怎么样?顺便说一句,您确实应该更改模型的定义。我建议您彻底阅读datamapper文档

class Task
  include DataMapper::Resource

  belongs_to :list
end

class List
  include DataMapper::Resource

  has n, :tasks
end

这样是不是看起来好看多了?哦,我希望你定义了键。这些对于运作良好的协会很重要。如果 idTask 的键,您的查询将简化为 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.

class Task
  include DataMapper::Resource

  belongs_to :list
end

class List
  include DataMapper::Resource

  has n, :tasks
end

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 for Task your query would simplify to Task.get(1).list.

醉南桥 2024-10-19 04:33:32

您可以使用这样的嵌套条件:

List.first(:tasks => { :id => task.id })

但给定一个任务,使用 task.list 会更简单

You can used a nested condition like this:

List.first(:tasks => { :id => task.id })

but given a task it would be simpler to use task.list

雨轻弹 2024-10-19 04:33:32

为什么你有1通过资源?我会先执行 Task.belongs_to :list 然后执行 List.has n, :tasks 这样你就可以写:

List.first :"tasks.id" => task.id

虽然如果你已经得到了任务,那么写 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:

List.first :"tasks.id" => task.id

Although if you already got the task then it's simpler to just write task.list :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文