1

I have Job and Feedback models.

They are associated like this:

Job has_many :feedbacks Feedback belongs_to :job

I'm trying to make a query to get jobs which have NO feedbacks with feedback.user_id == job.client_id

2
  • Paste the code what exactly you are trying to do Commented May 26, 2015 at 11:26
  • @user123 jobs_as_worker = current_user.jobs_as_worker.includes(:feedbacks).where{client_id.not_in Feedback.select{user_id}.uniq} Commented May 26, 2015 at 11:42

4 Answers 4

1
 jobs=Job.find(:all, :select => 'DISTINCT id', :order=>"id asc").map { |n| n.id.to_s })
 feedbacks=Feedback.find(:all, :select => 'DISTINCT job_id', :order=>"job_id asc").map { |n| n.job_id.to_s })
 jobs_without_feedbacks=jobs-feedbacks

ok. then try this. it might work good in your case. jobs_without_feedbacks will be the array of ids of the job with no feedback.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't need jobs without ANY feedbacks. I need jobs without feedbacks with user_id == job.client_id. If job has feedbacks with any other user_id – it's OK.
0
jobs=Job.all
no_feedback_jobs=Array.new
jobs.each do |job|{ (no_feedback_jobs<< job) if !job.feedback}

Try this .It will work fine

1 Comment

That works but I want to do it in the database, because it will be very slow if I have a lot of records.
0
feedbacks = Feedback.map(&:user_id).uniq

User.includes(:jobs => :feedbacks).where.not(:client_id => feedbacks).where(:id => current_user.id)

Try this

7 Comments

I suppose this is the same. jobs_as_worker = current_user.jobs_as_worker.includes(:feedbacks).where{client_id.not_in Feedback.select{user_id}.uniq} This code works but not in the way I need. I need to use not all feedbacks in databse – Feedback.select{user_id}.uniq, but feedbacks only for a specific job, like jobs.feedbacks.select{user_id}.uniq – but I can't make this work.
No, I want jobs which have NO feedbacks with feedback.user_id == job.client_id
Then in your code why you have written current_user.jobs_as_worker?
I want jobs that belong to current_user
I was asking the same question
|
0

This uses two queries, but the pluck should be relatively light.

Job.includes(:feedbacks)
  .where(feedbacks: {id: nil})
  .where.not(client_id: Feedback.pluck(:user_id))

6 Comments

What .where(feedbacks: {id: nil}) means? How id could be nil?
Job.includes(:feedbacks).where(feedbacks: {id: nil} returns jobs which do not have feedbacks.
This query returns Jobs where its client has feedbacks. I need Jobs where it's client didn't get feedback for this specific job.
I need something like this: .includes(:feedbacks).where(feedbacks: {id: nil}).where.not(feedbacks: {user_id: job.client_id}) – but I can't make it work.
How about adding a not to the last line?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.