数据映射器未执行正确的查询
我正在尝试执行一个简单的查询,但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有使用正确的参数,如果您想在关联上添加条件,您应该使用点符号。我在这里为您制作了一个示例脚本:
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
关系代码中可能存在错误(看起来您正在使用嵌套属性插件?)
DataMapper 的一个好处是您可以检查正在生成的查询。只需将“.query”标记到任何集合的末尾,您就会得到查询对象。
Answer.all(:user=>{:name=>'tom'}, :topics => [{:name => 'aboutme'}]).query
在这种情况下。您还可以通过执行以下操作来查看查询将生成什么 SQL:
试一试并回发它是否做了一些奇怪的事情(或者甚至发布正在生成的查询对象,我可以看一下)。
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:
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).