开发者

Querying empty Datamapper has n relationships [duplicate]

This question already has answers here: 开发者_运维知识库 Get the inverse of a join? (3 answers) Closed 2 years ago.

In my Rails app I have two models: Briefing and Company. Briefing has n, :companies, :through => Resource and Company has n, :briefings, :through => Resource.

I'm trying to find all the briefings that have don't have any companies associated with them. As incorrect code this might look something like:

Briefing.all( :companies => nil )

or

Briefing.all( :companies.eql => [] )

but of course those don't work. Any thoughts?


I believe Dan just pushed a change that makes possible to run such a query. So if you have a User.has n, :posts and you run a query like that:

User.all(:posts => nil)

Then the generated SQL will look like that:

SELECT "id" FROM "users" WHERE NOT("id" IN (SELECT "user_id" FROM "posts")) ORDER BY "id"

This will be available in DataMapper 1.1.1

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜