0

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.

My User Model has_many :accounts and my Account belongs_to :user

In the rails console when I do, e.g:

account = Account.first
account.user_id = [1, 2]
account.save

it returns true but when I check it again account.user_id = nil

How should I go on about it, then?

EDIT:

The Account Model:

class Account < ActiveRecord::Base

  belongs_to :user

  has_many :products

  validates_presence_of :title

end

and the User model:

class User < 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

EDIT 2:

I've updated the models relationship so the new Account model is:

class Account < ActiveRecord::Base

  has_and_belongs_to_many :users

  validates_presence_of :title

end

and the new User model is:

class User < 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

I also created a new migration, a accounts_users table

class CreateAccountsUsersJoinTable < 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

Still I can't have a account with multiple users

tvieira
  • 1,865
  • 3
  • 28
  • 45
  • Are you sure that `account.user_id = [1, 2]` is appropriate? – sawa Jan 09 '14 at 16:46
  • 2
    You have to either reverse your association (`User belongs_to :account` / `Account has_many :users`) or use a [many-to-many relationship](http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many). – Stefan Jan 09 '14 at 16:50
  • Don't know and not sure about anything at this point, as this is a new level of complexity for me – tvieira Jan 09 '14 at 16:50
  • Seems like associations are playing with your mind :) Can you show us models of Account and User? – bbozo Jan 09 '14 at 17:20
  • I added the User and Account models – tvieira Jan 09 '14 at 17:40
  • 3
    What Stefan said is correct. Here is [another SO answer](http://stackoverflow.com/a/5120734/877472) that might help with the many-to-many part. – Paul Richter Jan 09 '14 at 17:50
  • the only thing I'm not sure is that, should the account table have an user_id column and the user table have an account_id column if I do the join table? – tvieira Jan 09 '14 at 18:09
  • I updated the post, with some changes I did – tvieira Jan 09 '14 at 20:29
  • I recommend you look in the Rails guides and find where it talks about `has_and_belongs_to_many`. Read what it says and follow carefully. – Alex D Jan 09 '14 at 20:42
  • Sorry my mistake, I'm not supposed to use has_and_belongs_to_many method, since I created a join table I switched to has_many and belongs_to – tvieira Jan 09 '14 at 20:48
  • @TiagoOrnelasVieira Why are you not allow to use `has_and_belongs_to_many`? – Paul Richter Jan 09 '14 at 20:53
  • I'm exploring the options, I have no particular reason. I read on the documentation that has_many or belongs_to it's the most common way of doing – tvieira Jan 09 '14 at 21:10
  • If User belongs_to Account, then you are NOT using a join table, you would have account_id ON the User table... – omarvelous Jan 09 '14 at 22:14
  • yes I added a column for account_id in the user table and created a join table (I switched back to has_and_belongs_to_many method) – tvieira Jan 09 '14 at 22:21

0 Answers0