开发者

How do I do an Intersection of many<-->many DataMapper Collection?

开发者 https://www.devze.com 2022-12-15 08:31 出处:网络
I am trying to return an intersection of datamapper collections relating to tagged topics. Before we go any further I should point out the obvious:

I am trying to return an intersection of datamapper collections relating to tagged topics.

Before we go any further I should point out the obvious:

@tags = Tag.all(:title => ["shim", "sham"])
@tags.topics

This returns a UNION which means I get all topics with either 'shim' or 'sham' as a tag.

What I want to do is return all articles that have BOTH 'shim' AND 'sham' as tags.

@tags = Tag.all(:title => ["shim","sham"])
@blah = []
@blah << @tags.topics.first
@tags.each do |tag| @blah = @blah & tag.topics end

Ok, we have our data -- now we still have one problem. We need to have this as a DataMapper collection so I can drilldown my results effectively like so:

@topics = @blah(:order => [:created_at.desc], :limit => limit, :offset => offset)

This of course is not possible since @blah is now an array and not a DataMapper Collection.

a more succint version of the above:

@topics = Tag.all(:title => ["shim"]).topi开发者_开发百科cs & Tag.all(:title => ["sham"]).topics

although we still end up with an array..... according to http://www.mail-archive.com/datamapper@googlegroups.com/msg02092.html this should be possible


This is how I'm accomplishing it:

words = query.split /,/

tags = Tag.all :label.in => words

photos = tags.shift.photos
tags.each do |tag|
  @items &= tag.photos
end

It seems like there should be a more succinct way to do this, but it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消