title: How can two user instances have the same account
body: <p>In my rails app I want to create a shared account (later with different access levels). I want two instances of the User to have the same account.</p>
  
  <p>My User Model <code>has_many :accounts</code> and my Account <code>belongs_to :user</code> </p>
  
  <p>In the rails console when I do, e.g:</p>
  
  <pre><code>account = Account.first
  account.user_id = [1, 2]
  account.save
  </code></pre>
  
  <p>it returns true but when I check it again <code>account.user_id = nil</code> </p>
  
  <p>How should I go on about it, then?</p>
  
  <p>EDIT:</p>
  
  <p>The Account Model:</p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
  
    belongs_to :user
  
    has_many :products
  
    validates_presence_of :title
  
  end
  </code></pre>
  
  <p>and the User model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :accounts
    has_many :products, through: :accounts
  
    accepts_nested_attributes_for :accounts
  
    validates_presence_of :name
  
    before_save do
      self.email = email.downcase unless :guest?
    end
  
    validates :name, presence: true, length: { maximum: 50 }, unless: :guest?
  
  end
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>I've updated the models relationship so the new Account model is:</p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
  
    has_and_belongs_to_many :users
  
    validates_presence_of :title
  
  end
  </code></pre>
  
  <p>and the new User model is:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_and_belongs_to_many :accounts
    belongs_to :account
    has_many :products, through: :accounts
  
    accepts_nested_attributes_for :accounts
  
    validates_presence_of :name
  
    before_save do
      self.email = email.downcase unless :guest?
    end
  
    validates :name, presence: true, length: { maximum: 50 }, unless: :guest?
  
  end
  </code></pre>
  
  <p>I also created a new migration, a accounts_users table</p>
  
  <pre><code>class CreateAccountsUsersJoinTable &lt; ActiveRecord::Migration
    def change
      create_join_table :users, :accounts do |t|
         ## Not sure if this is necessary
         ##t.index [:user_id, :account_id]
         ##t.index [:account_id, :user_id]
         t.belongs_to :user
         t.belongs_to :account
      end
    end
  end
  </code></pre>
  
  <p>Still I can't have a account with multiple users</p>