Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails query not in statement formatting correct

I do this:

    check1 = inquiries.select("question_id").where(:membership_id => self.id)

This returny a active Record array with entries like this:

#<ActiveRecord::AssociationRelation [#<Inquiry id: nil, question_id: 21113>,

Now I want to select in a secon query all questions which are not in the inqueries

Tried this:

lesson.questions.where("NOT IN ?",check1)

But that does not work, probably because the first query returns an active record array with 2 values for each object?

how could look like a solution?

like image 242
Felix Avatar asked Jan 24 '26 14:01

Felix


1 Answers

Make use of pluck

check1 = inquiries.where(membership_id: self.id).pluck(:question_id)
#=> [1, 2, 3] # List of ids 

lesson.questions.where("id NOT IN (?)", check1)

# OR 

lesson.questions.where.not(id: check1)

NOTE: If you have an association between lesson and inquiries you can make use of joins and get the result in one query itself.

like image 192
Deepak Mahakale Avatar answered Jan 26 '26 06:01

Deepak Mahakale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!