数据映射器未执行正确的查询

发布于 2024-10-03 23:21:59 字数 806 浏览 1 评论 0原文

我正在尝试执行一个简单的查询,但 Datamapper 似乎没有返回正确的结果集。

这看起来很基本,没有理由说它是错误的。

我认为这可能是一个语法问题。

class User
 has n, :answers
 property :id, Serial
 property :name, String
end

class Answer
 belongs_to :user
 has n, :topics, :through => Resource
 property :id, Serial
 property :text, Text
end

class Topic
 has n, :answers, :through => Resource
 property :name, String, :key => true
end

o=User.create(:name=>'tom')
puts a=Answer.create(:user=>o, :text => 'a1', :topics => [
 Topic.first_or_create(:name => 'aboutme'),
 Topic.first_or_create(:name => '@onetom')
])

#THIS WORKS
#puts Answer.all(:user => {:name => 'tom'}, :topics => [{:name => 'aboutme'}])

#THIS DOES NOT WORK
#puts o.answers.all(:topics => [{:name => 'aboutme'}])

I am trying to perform a simple query, but Datamapper does not seem to be returning the right result sets.

This seems so basic that there is no reason why it is wrong.

I think it is probably a syntax issue.

class User
 has n, :answers
 property :id, Serial
 property :name, String
end

class Answer
 belongs_to :user
 has n, :topics, :through => Resource
 property :id, Serial
 property :text, Text
end

class Topic
 has n, :answers, :through => Resource
 property :name, String, :key => true
end

o=User.create(:name=>'tom')
puts a=Answer.create(:user=>o, :text => 'a1', :topics => [
 Topic.first_or_create(:name => 'aboutme'),
 Topic.first_or_create(:name => '@onetom')
])

#THIS WORKS
#puts Answer.all(:user => {:name => 'tom'}, :topics => [{:name => 'aboutme'}])

#THIS DOES NOT WORK
#puts o.answers.all(:topics => [{:name => 'aboutme'}])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

み青杉依旧 2024-10-10 23:21:59

您没有使用正确的参数,如果您想在关联上添加条件,您应该使用点符号。我在这里为您制作了一个示例脚本:

https://gist.github.com/709867

You're not using correct parameters, if you want to add conditions on associations you should use the dot notation. I made an example script for you here:

https://gist.github.com/709867

我三岁 2024-10-10 23:21:59

关系代码中可能存在错误(看起来您正在使用嵌套属性插件?)

DataMapper 的一个好处是您可以检查正在生成的查询。只需将“.query”标记到任何集合的末尾,您就会得到查询对象。

Answer.all(:user=>{:name=>'tom'}, :topics => [{:name => 'aboutme'}]).query 在这种情况下。

您还可以通过执行以下操作来查看查询将生成什么 SQL:

q = Answer.all(:user=>{:name=>'tom'}, :topics => [{:name => 'aboutme'}]).query
DataMapper.repository.adapter.send(:select_statement, q)

试一试并回发它是否做了一些奇怪的事情(或者甚至发布正在生成的查询对象,我可以看一下)。

There is a possibility of there being a bug in the relationships code (it looks like you're using the nested attributes plugin?)

One nice thing about DataMapper is that you can check what query is being generated. Just tag ".query" onto the end of any collection and you'll get the query object out.

Answer.all(:user=>{:name=>'tom'}, :topics => [{:name => 'aboutme'}]).query in this case.

You can also see what SQL the query would generate by doing the following:

q = Answer.all(:user=>{:name=>'tom'}, :topics => [{:name => 'aboutme'}]).query
DataMapper.repository.adapter.send(:select_statement, q)

Give that a shot and post back about whether it's doing something strange or not (or even post the query objects being generated and i can take a look).

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