Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:09:29+00:00 2026-06-15T16:09:29+00:00

My application has a Job model. Every job in the system has a contact

  • 0

My application has a Job model. Every job in the system has a contact. This is like a person you would call up if you need to ask a question about the job. A contact can either be a client or an employee of a client (ClientEmployee).

class Job < ActiveRecord::Base
  belongs_to :contact, polymorphic: true
end

class Client < ActiveRecord::Base
  has_many :jobs, as: :contact
  has_many :employees, class_name: 'ClientEmployee'
end

class ClientEmployee < ActiveRecord::Base
  belongs_to :client
  has_many :jobs, as: :contact
end

Clients have the idea of commissioned_jobs. The clients commissioned jobs are those jobs for which the client is the contact OR one of the client’s employees is the contact.

class Client < ActiveRecord::Base
  has_many :jobs, as: :contact
  has_many :employee_jobs, through: :employees, source: :jobs

  def commissioned_jobs
    jobs << employee_jobs
  end
end

Aside: That method is a bit of a hack because it returns an array rather than an ActiveRecord::Relation. It’s also interesting that it blows up if I try to concat jobs into employee_jobs. It may or may not do for my purposes.

I would like to add a scope to Client called with_commissioned_jobs. This should return all the clients in the system who have jobs or who have employees who have jobs.

class Client < ActiveRecord::Base
  def self.with_commissioned_jobs
    # I can get clients with jobs using: joins(:jobs). How do 
    # I also include clients with employees who have jobs?
  end
end

How do I implement this method?

I’m using Rails 3.2.9.

Update:

I’ve made some progress and I now have two methods, each of which does half of what I need.

class Client < ActiveRecord::Base
  # Return all clients who have an employee with at least one job.
  def self.with_employee_jobs
    joins(employees: :jobs)
    # SQL: SELECT "clients".* FROM "clients" INNER JOIN "client_employees" ON "client_employees"."employer_id" = "clients"."id" INNER JOIN "jobs" ON "jobs"."contact_id" = "client_employees"."id" AND "jobs"."contact_type" = 'ClientEmployee'
  end

  # Return all clients who have at least one job.
  def self.with_jobs
    joins(:jobs)
    # SQL: SELECT "clients".* FROM "clients" INNER JOIN "jobs" ON "jobs"."contact_id" = "clients"."id" AND "jobs"."contact_type" = 'Client'
  end
end

Now all I need to do is combine these two method calls into one ActiveRecord::Relation. I can obviously do this:

  def self.with_commissioned_jobs
    with_jobs + with_employee_jobs
  end

The problem is that that returns an array rather than an instance of Relation and I can’t chain more scopes on it.

Update 2:

Using merge doesn’t appear to work either. Here is the AR query and the resulting SQL.

joins(:jobs).merge(joins(employees: :jobs))

SELECT "clients".* FROM "clients" INNER JOIN "jobs" 
  ON "jobs"."contact_id" = "clients"."id" 
  AND "jobs"."contact_type" = 'Client' 
  INNER JOIN "client_employees" 
  ON "client_employees"."employer_id" = "clients"."id" 
  INNER JOIN "jobs" "jobs_client_employees" 
  ON "jobs_client_employees"."contact_id" = "client_employees"."id" 
  AND "jobs_client_employees"."contact_type" = 'ClientEmployee'

By the way, here are the tests I’m trying to pass. The first test fails because there are zero results when I use merge.

describe "with_commissioned_jobs" do
  # A client with a job.
  let!(:client_with) { create :client }
  let!(:job) { create :job, contact: client_with }
  # A client who does not himself have a job, but who has an employee
  # with a job.
  let!(:client_with_emp) { create :client }
  let!(:employee) { create :client_employee, employer: client_with_emp }
  let!(:emp_job) { create :job, contact: employee }
  # A client with nothing. Should not show up.
  let!(:client_without) { create :client }

  it "should return clients with jobs and clients with employee jobs" do
    Client.with_commissioned_jobs.should == [client_with, client_with_emp]
  end

  it "should return a relation" do
    Client.with_commissioned_jobs.should be_instance_of(ActiveRecord::Relation)
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:09:31+00:00Added an answer on June 15, 2026 at 4:09 pm

    Have you considered the gem meta_where? The main thing seems to be that you want to return an ActiveRecord:Relation object for further chaining.

    UPDATE 2: got it working with LEFT OUTER JOIN jobs twice with aliasing

      # scope for ::Client
      def self.with_commissioned_jobs
        self.joins("LEFT OUTER JOIN client_employees ON clients.id =client_employees.client_id").
            joins("LEFT OUTER JOIN jobs AS cjobs ON clients.id = cjobs.contact_id AND cjobs.contact_type = 'Client'").
            joins("LEFT OUTER JOIN jobs AS ejobs ON client_employees.id = ejobs.contact_id AND ejobs.contact_type = 'ClientEmployee'").
            where("cjobs.id IS NOT NULL OR ejobs.id IS NOT NULL")
      end
    

    Seeing if it works:

        #c1 has no job
        c1 = Client.create
    
        #c2 has a job
        c2 = Client.create
        c2.jobs.create
    
        #c3 has no job, but has an employee with a job
        c3 = Client.create
        c3.employees.create
        c3.employees.first.jobs.create
    
        puts Client.all.inspect             #=> [#<Client id: 1>, #<Client id: 2>, #<Client id: 3>] 
        puts Client.with_commissioned_jobs  #=> [#<Client id: 2>, #<Client id: 3>]
    
        puts [c2,c3] == Client.with_commissioned_jobs.all    #=> true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has multiple SQLite tables(around 20 with different fields). This I have created
My application has a simple data model with 70 read-only records grouped into six
I would like to implement an (open source) web application, where the user sends
Have just started converting an existing job tracking system into an ASP.NET MVC application.
I need to know the total number of threads that my application has spawned
A Symfony2 application has a Job entity which has a tasks property, a collection
The question doesn't really concerne DDD but would like to know if there is
I have a dll that has an entity model that does a particular job.
I know this question has been asked in several variations before, but my question
The ASP.NET application that I am currently responsible for at my day job has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.