Complicated named_scope: Find users who don't belong to a certain project?

Thinking in SQL, the query should be something like: select id from users where id not in (select id from users join duties on users. Id = duties. User_id join projects on duties.

Project_id = projects. Id where projects. Id = %) But I'm not too sure how it would work using named_scope.

I'd say use something like def self. Not_present_in p find_by_sql "select id from users where id not in (select id from users join duties on users. Id = duties.

User_id join projects on duties. Project_id = projects. Id where projects.Id =?

)", p end Not as pretty as using AR, but will work (and save you some queries, probably).

Thanks Yaraher, the sql query you provide works as I want with a minor fix. I had to substitute "id" with "users. Id" after the second select for not to be ambiguous.

However, I cannot use a find_by_sql in a named_scope :(. – Manuel M Sep 8 '09 at 7:33 But why do you need it to be a named scope? For merging it with another methods?

Unless that's the case, it should work fine. And if you do want to refine it a bit later, you could always submit an options hash akin of how AR uses, and concatenating/merging it with the find_by_sql query. Could be a bit of extra work but it will be probably as close as you can get.

ORMs not always keep us from using SQL :) – Yaraher Sep 8 '09 at 12:24.

From users join duties on users.id = duties. Join projects on duties. Find_by_sql "select id from users where id not in (select id from users join duties on users.id = duties.

User_id join projects on duties.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions