Question

I have a many-to-many relationship between Users table and Accounts table, implemented with the :has_many, :through method. Users can have multiple accounts and accounts can have multiple users.

I have a form where the user selects all the accounts he wants to be associated with. I want to associate the accounts to the User object so that I can query @user.accounts and have an updated list.

class User
   has_many :accountings
   has_many :accounts, :through => :accountings

class Neighborhood   
   has_many :accountings   
   has_many :users, :through => :accountings

class Accountings
   belongs_to :user
   belongs_to :accountings

How should I implement the form that sends the account IDs and updates the db?

I feel like this may be a basic question but am looking for the Rails way to do things. Thanks in advance.

Was it helpful?

Solution

Solved it using the following for checkboxes (angular):

    <input type="checkbox" name="user[account_ids][]"
    value="{{ hood.id }}">{{ hood.name }} </div>

This will update the model's many-to-many relationship so that I can query user.account_ids and get an updated list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top