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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:28:58+00:00 2026-06-17T11:28:58+00:00

I have setup a has_many through association between two models in Ruby on Rails.

  • 0

I have setup a has_many through association between two models in Ruby on Rails. The setup is as follows. The models are User and Document, the join model is Ownership. The models are defined like this:

class Ownership < ActiveRecord::Base
  attr_accessible :document_id, :user_id 
  belongs_to :user
  belongs_to :document 
end

class User < ActiveRecord::Base
  has_many :ownerships
  has_many :documents, :through => :ownerships
end

class Document < ActiveRecord::Base
  has_many :ownerships
  has_many :users, :as => :owners, :through => :ownerships
end

Now my question is how to set the user that creates a document as the owner of the document when it gets created. The project also uses devise, cancan and rolify for user handling. I tried to set it in the new action of the Codument controller like this but with no successs

def new 
  @document = Document.new

  @document.users = current_user

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @document }
  end 
end 

How can I do this properly? And is the new action of my Document controller the right place at all to something like this? Any help would be appreciated.

  • 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-17T11:28:58+00:00Added an answer on June 17, 2026 at 11:28 am

    First off, you need to assign the user in the controller’s create method. Second, since a document can have many users, @document.users is an enumerable and cannot simply be assigned a single user by doing

    @document.users = current_user
    

    You can rather do:

    @document.owners << current_user
    

    in the create method. Note that as per your model the document has owners rather than users.

    Change

    has_many :users, :as => :owners, :through => :ownerships
    

    to

    has_many :owners, source: :user, through: :ownerships, foreign_key: :user_id
    

    in your document model.

    This stores the current user when the document is saved.

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

Sidebar

Related Questions

I have a has_many :through association setup between two tables (Post and Category). The
I have a has_many through association setup between a song model and an artist
I'm trying to setup a simple has_many :through => association. I have 3 models.
I have a typical model setup: Store has_many Items Orders has_many Items :through OrderItems
I setup a has_many through association with my models and I am trying to
I have a join model in a HABTM relationship with a through association (details
I have three Models setup with the following associations class User < ActiveRecord::Base has_many
I have my models & associations currently setup like so: class User < ActiveRecord::Base
I have the following setup in my rails app: A user registers and he
I have setup a model structure which allows different models to associate with a

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.