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

vladzaets
  • 53
  • 10

4 Answers4

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.

sreeraj nyros
  • 929
  • 1
  • 6
  • 17
  • 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. – vladzaets May 26 '15 at 12:14
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

sreeraj nyros
  • 929
  • 1
  • 6
  • 17
  • That works but I want to do it in the database, because it will be very slow if I have a lot of records. – vladzaets May 26 '15 at 11:40
0
feedbacks = Feedback.map(&:user_id).uniq

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

Try this

Vrushali Pawar
  • 3,753
  • 1
  • 13
  • 22
  • 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. – vladzaets May 26 '15 at 11:55
  • No, I want jobs which have NO feedbacks with `feedback.user_id == job.client_id` – vladzaets May 26 '15 at 12:05
  • Then in your code why you have written current_user.jobs_as_worker? – Vrushali Pawar May 26 '15 at 12:06
  • I want `jobs` that belong to `current_user` – vladzaets May 26 '15 at 12:08
  • I was asking the same question – Vrushali Pawar May 26 '15 at 12:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78796/discussion-between-user123-and-vladzaets). – Vrushali Pawar May 26 '15 at 12:17
  • I didn't ask any questions. I need current_user's jobs which DON'T have feedbacks with specific user_id. Any other user_id's are ok. – vladzaets May 26 '15 at 12:18
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))
fylooi
  • 3,840
  • 14
  • 24
  • What `.where(feedbacks: {id: nil})` means? How `id` could be `nil`? – vladzaets May 26 '15 at 12:26
  • `Job.includes(:feedbacks).where(feedbacks: {id: nil}` returns jobs which do not have feedbacks. – fylooi May 26 '15 at 12:30
  • This query returns Jobs where its client has feedbacks. I need Jobs where it's client didn't get feedback for this specific job. – vladzaets May 26 '15 at 12:33
  • 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. – vladzaets May 26 '15 at 12:37
  • How about adding a `not` to the last line? – fylooi May 26 '15 at 12:46
  • It returns only 1 job, but I expect to get 3. That is because of `Feedback.pluck(:user_id)`. I need to use something like `job.feedbacks.pluck(:user_id)` but I don't know if it is possible – vladzaets May 26 '15 at 13:16