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'}])
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