2010-11-21 109 views
1

我想执行一个简单的查询,但Datamapper似乎没有返回正确的结果集。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'}]) 

回答

3

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

https://gist.github.com/709867

+0

谢谢!问题解决了! = d – 2010-11-22 15:04:08

0

有有是在关系代码中的错误的可能性(它看起来像你使用嵌套属性插件?)

约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) 

给予一个开枪后回到它是否在做一些奇怪的或不(或者甚至发布的查询对象生成我可以看看)。