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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:24:25+00:00 2026-06-13T00:24:25+00:00

I have three classes: Person, Position, and Directory. A Person has_many :directories, :through =>

  • 0

I have three classes: Person, Position, and Directory.

  • A Person has_many :directories, :through => :position.
  • A Directory has_many :people, :through => :position.
  • Both Person and Directory has_many :positions.
  • The Position model, in addition to having an id, a person_id, and a directory_id, has one or more additional fields (e.g., title).

What I would like to be able to do is add data to the join model, such as the title field, every time I add a person to a Directory.people collection. The usual << operator won’t cut it. Id est:

directory = Directory.last     # Let's assume that retrieves a Directory object
person = Person.last           # Let's assume that retrieves a Person object
directory.people << person

This will add a person to the people collection of a Directory object, but will not allow me the chance to assign data to the join model. So, after doing a lot of research on this site, I found another way to add the Person to the people collection and add data to the Position that links the Person and the Directory, id est:

directory = Directory.last     # Let's assume that retrieves a Directory object
person = Person.last           # Let's assume that retrieves a Person object
position = person.positions.build(:directory_id => directory.id, :title => "Administrative Assistant") 
position.save

This is cumbersome. An equally cumbersome way would be:

directory = Directory.last     # Let's assume that retrieves a Directory object
person = Person.last           # Let's assume that retrieves a Person object
position = Position.new(directory_id: directory.id, person_id: person.id, title: "Administrative Assistant")

Again, seems wrong because I’d like to be able to emphasize the relationship between Person and Directory, which I believe is what makes using has_many :through appropriate.

What I’d like to be able to do is use the << operator, and just pass the additional data, e.g.:

directory = Directory.last # Let's assume that retrieves a Directory object
person = Person.last # Let's assume that retrieves a Person object
directory.people << person, :position => {:title => "Administrative Assistant"}

I have overloaded the << operator in my has_many :through declaration, as follows:

has_many :people, :through => :positions do
  def << *args
    arg = args.first
    if arg.is_a?(Person)
      self.push([arg]) 
    elsif arg.is_a?(Hash)
      # Don't know what to do in here (see below)
    else
      raise "Invalid Value" # There's a better error to raise here, but good enough for now.
    end
  end
end

The advantage of getting this working is that it works well syntactically, and allows me to concisely assign data to the join object (a Position) while adding the Person to the people collection of the Directory object.

But I cannot make it work because I would need to be able to access the Directory object of which the people collection on the left side of the << operator is an attribute, in order to build a Position and save it to the database.

So, my questions are:

  1. Is there a way to access an object from an attribute of an object?
  2. In the alternative, is there another way to overload the << operator so that I can easily assign data to the join model while adding one object to a collection?

Thanks very much for your help and thoughtful responses. I’ve been hacking away at this for half a day to no avail.

Answer
Thanks to PinnyM, who answered this question, I was able to come up with this implementation:

module AddToPeopleAndPositionExtension
  def << *args
    arg = args.first
      if arg.is_a?(Person)
        self.push([arg]) 
        return self
      elsif arg.is_a?(Hash)
        directory = proxy_association.owner
        person = arg[:person]
        position = person.positions.build(:directory_id => directory.id, :title => arg[:position][:title]) 
        position.save
      else
        raise "Invalid Value"
      end
  end
end

class Directory < ActiveRecord::Base  
  # Relationships
  has_many :positions
  has_many :people, :through => :positions, :extend => AddToPeopleAndPositionExtension
end

This allowed me to call the << operator in the standard way if I did not care what happens on the join model, like:

directory = Directory.last     # Let's assume that retrieves a Directory object
person = Person.last           # Let's assume that retrieves a Person object
directory.people << person

And, I could also call it in a way that specified attributes of the join model like:

directory = Directory.last     # Let's assume that retrieves a Directory object
person = Person.last           # Let's assume that retrieves a Person object
directory.people << {:person => person, :position => {:title => "Administrative Assistant"}}
  • 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-13T00:24:27+00:00Added an answer on June 13, 2026 at 12:24 am

    You can use the proxy_association helper inside the block to get the association and proxy_association.owner to get the Directory object itself. See here for some more info on this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# I have three classes: Person, Cat, and Dog. Both the Cat and
I have three classes; Classes A and B both reference class C . How
I have three classes Class Company(models.Model): name = CharField( max_length = 26 , blank
Lets say I have three django model classes - lets call them A, B
I have four classes. Class Person , and three more, Student , Professor ,
I have three classes: class A { public Object Person; } class B extends
I have a three classes: First is Person: public class Person { public string
I have three simple classes for illustration. People have many Projects which have many
I have two objects of classes Person and Employee. Both classes have common attribute
I have there domain classes: Person. (Person.ID, Name,Address) Designation.(Designation.ID, Title, Band) SalarySlip (Person.ID, Designation.ID,

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.