1

Basically I have 3 tables. Ignore the syntax. I just wanted to show the idea.

ModelA:
    belongs_to :ModelB

ModelB:
    has_many: ModelA
    has_and_belongs_to_many :ModelC

ModelC
    has_and_belongs_to_many :ModelB

How can I get all the rows from ModelA where all rows of ModelC have flag = 'Y'?
I've tried:

ModelA.joins(:ModelB => :ModelC).where("#{ModelC.table_name}.flag = 'Y'")

and dozen other ways without success. I know the code above doesn't check all the rows, but that's as far as I've got. I would prefer using ActiveRecord.

1 Answer 1

0

Their is a solution which will solve your problem but its not a perfect one

ModelA.all - ModelA.joins(:ModelB => :ModelC).where("#{ModelC.table_name}.flag != 'Y'")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.