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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:12:44+00:00 2026-06-01T06:12:44+00:00

I have two tables with a many to many relationship, through a third table.

  • 0

I have two tables with a many to many relationship, through a third table. In the third table is a piece of data I need to assign when I build the relationships between the two tables, how can I use ActiveRecords build method to assign that?

Here is code to show what I mean:

class Company < Contact

  has_many :contact_companies
  has_many :people, :through => :contact_companies
  accepts_nested_attributes_for :people, :allow_destroy => true
  accepts_nested_attributes_for :contact_companies

end


class Person < Contact

  has_many :contact_companies
  has_many :companies, :through => :contact_companies
  accepts_nested_attributes_for :companies, :allow_destroy => true
  accepts_nested_attributes_for :contact_companies
end


class ContactCompany < ActiveRecord::Base
  belongs_to :person
  belongs_to :company
end

ContactCompany contains a data member called “position”. What I want to do is something like:

c = Person.new
c.companies.build(:name => Faker::Company.name, :position => positions.sample)

EDIT:

When I try the code above I get “unknown attribute: position”.

  • 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-01T06:12:45+00:00Added an answer on June 1, 2026 at 6:12 am

    The c.companies.build line is attempting to build a Company object which does not have the position attribute (the ContactCompany does) hence the error. It looks like you are trying to set attributes on two different models, so you’ll have to make sure you are setting the appropriate attribute on the right model:

    # you can chain these calls but I separated them for readability
    cc = c.contact_companies.build(:position => positions.sample)
    cc.build_company(:name => Faker::Company.name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one to many relationship between two tables. The many table contains
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have two tables (Rule and Object) linked through a many-to-many relationship. A Rule
I have two tables with a many-to-one relationship. (Oracle) **Table: PARENT** Field: A (PK)
I have two tables (renamed/refactored for illustrative purposes) with a Many-To-Many relationship in an
If I have a relationship between two tables (both tables have their own primary
I have two tables linked together through the 3rd table threads: id, name tags:
I have two models that has a manytomany relationship with a 'through' table in
I have two tables in a one to many relationship. (products and qty break
I have two domain classes with a many-2-many relationship between them, e.g. User and

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.