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 7772819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:58:03+00:00 2026-06-01T16:58:03+00:00

I have a pretty simple set of data requirements to do with pets and

  • 0

I have a pretty simple set of data requirements to do with pets and veterinarians.

  • An owner can have many pets
  • A pet can have one owner
  • A pet can be treated by (belongs_to) many veterinarians
  • A veterinarian can treat (have_many) pets

Owner and Veterinarian are both subclasses of User using STI.

Here’s some code:

class Owner < User
  has_many :pets
  has_many :veterinarians, :through => :pets
end

class Veterinarian < User
  attr_accessible :clinic_name

  has_many :pets
  has_many :owners, :through => :pets
end


class Pet < ActiveRecord::Base
  attr_accessible :name, :date_of_birth, :species, :breed, :gender, :neutered

  belongs_to :owner
  belongs_to :veterinarian
end

And here is the spec that is failing:

it "has various veterinarians" do
  o = Owner.make!(:email => 'owner1@gmail.com')
  v1 = Veterinarian.make!(:email => 'vet_1@gmail.com')
  v2 = Veterinarian.make!(:email => 'vet_2@gmail.com')
  p = Pet.make!(:name => 'fluffy')

  o.pets << p
  v1.pets << p
  v2.pets << p

  o.pets.should have(2).records
  o.veterinarians.should have(2).records
end

The make! stuff is to do with using machinist fixture replacement. It just factory creates the objects.

The failure occurs on the last line. It turns out that o.veterinarians only has 1 record. I understand that a pet is not a join table in the traditional sense, inasmuch as I don’t want to create a whole new pet each time I create a relationship between an owner and a veterinarian. Should I be using a schema more like Owner has_many Pets, Pet belongs_to :owner and Pet has_and_belongs_to_many Veterinarians?

Thanks!

  • 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-01T16:58:05+00:00Added an answer on June 1, 2026 at 4:58 pm

    You have to use a join table between the Pet and the Veterinarian. Let’s call it Treatment:

    class Treatment < ...
    ...
      belongs_to :pet
      belongs_to :veterinarian
    ...
    end
    
    class Veterinarian < ...
    ...
      has_many :treatments
      has_many :pets, through: :treatments
    ...
    end
    
    class Pet < ...
    ...
      has_many :treatments
      has_many :veterinarians, through: :treatments
    ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty simple JSP/Servlet 3.0/Spring MVC 3.1 application. On one of my
I have a pretty simple set up here, and Xcode is giving me a
This should be pretty simple, but I can't seem to get it. I have
I have a pretty simple set up for a messaging application. I simply get
I have pretty simple jquery code : $(document).ready(function(){ $('img.marqFl').on({ mouseenter: function() { $(this).animate({height: 300},
i have pretty simple simple question (i hope so). How do i change the
I have a pretty simple trigger: CREATE OR REPLACE FUNCTION f_log_datei() RETURNS TRIGGER AS
I have a pretty simple ASP.NET Web Form that looks a bit like the
I have a pretty simple Linq to XML query: var q = from c
I have a pretty simple SQL I need to perform. I have a ProcessUser

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.