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

  • Home
  • SEARCH
  • 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 4029766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:23:12+00:00 2026-05-20T11:23:12+00:00

Going back to an example I had previously asked about, I’ll try and make

  • 0

Going back to an example I had previously asked about, I’ll try and make this question as simple as possible.

Supposed I have User and Document models.

A User has_many Documents and a Document belongs_to a User. This relationship works fine today.

I want to introduce an Edits model that belongs_to a Document and belongs_to a User. The User can be any user, not necessary the one who created the document. With this new model, the Document now has_many :edits and the User has_many :edits.

So far it would look like:

# user.rb
class User < ActiveRecord::Base
   has_many :edits
   has_many :documents
end

# document.rb
class Document < ActiveRecord::Base
   belongs_to :user
   has_many :edits
end

# edit.rb
class Edit < ActiveRecord::Base
   belongs_to :user
   belongs_to :document
end

When I create a Document through a User, the association between the User and Document are fine in both directions (user.documents and document.user)

Now when I want to create an Edit, the edit should be against a Document but should also be associated with the user who generated the edit (edit.user).

When I’m building this in my RSpec tests I’m struggling to get the associations correct using the “standard” association methods. If I do @user.edits.build({…}) it will associate the user in the returned Edit object, but not the Document. Likewise when I do @document.edits.build({…}) it will associate the Document but not the User.

I supposed I could expose the user_id and post_id in the attr_accessible declaration but won’t this but I fear this is not a best way of doing this. I have no real reason to fear other than the attributes are now accessible through mass assignment (from what I understand).

Am I going about this the wrong way or is there a better way to create and test all the associations?

  • 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-05-20T11:23:13+00:00Added an answer on May 20, 2026 at 11:23 am
    class User < ActiveRecord::Base
       has_many :edits, :through => :documents
       has_many :documents
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Going back to my previous question on OCSP, does anybody know of reliable OCSP
I have been going back and forth between C# and Java for the last
A friend and I are going back and forth with brain-teasers and I have
I have been using C# for a while now, and going back to C++
While going through university and from following the development of SO, I've heard a
Been going over my predecessor's code and see usage of the request scope frequently.
After going through the Appendix A, C# Coding Style Conventions of the great book
What's going on here? printf.sh: #! /bin/sh NAME=George W. Bush printf Hello, %s\n $NAME
I'm going to start a new project - rewriting an existing system (PHP +
I am going to be using C/C++, and would like to know the best

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.