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.

ctpfaff
  • 59
  • 1
  • 9
  • I'm not understanding why you're doing a has_many through relationship. Can a document have multiple owners? – Preacher Jan 13 '13 at 14:31
  • Yes a document can have many owners not only one. – ctpfaff Jan 13 '13 at 14:35
  • So do you want the document creator to be identified as just one of the regular owners, or do you want them to be identified as THE specific owner/creator? – Preacher Jan 13 '13 at 14:39
  • I think I like to have the creator along with the other owners later on. I like to add the creator as first owner and later on add new owners if required. – ctpfaff Jan 13 '13 at 14:44

1 Answers1

1

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.

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
  • Thanks. It works when I do a (at)document.users << current_user but not when I use the line (at)document.owners << current_user. Does this mean there is something wrong with the alias setup over :as => :owners in the document model? – ctpfaff Jan 13 '13 at 15:19
  • I am not sure - have you tried `has_many :owners, class_name: :user, through: :ownerships`? – Dennis Hackethal Jan 13 '13 at 15:25
  • Hm, no. But I tested it now and it does not work either. It tells me: Could not find the source association(s) :owner or :owners in model Ownership. Try 'has_many'. – ctpfaff Jan 13 '13 at 15:39
  • Try `has_many :owners, class_name: :user, through: :ownerships, foreign_key: :user_id` – Dennis Hackethal Jan 13 '13 at 15:50
  • Unfortunateley no sucess with this either. This has the same error message. – ctpfaff Jan 13 '13 at 15:59
  • Got it: has_many :owners, source: :user, through: :ownerships, foreign_key: :user_id works. Thank you1 – ctpfaff Jan 13 '13 at 16:09
  • Great! I updated the answer accordingly. Glad we got it working :) – Dennis Hackethal Jan 13 '13 at 16:17