Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If i understood you correctly</p> <pre><code>class Accounts &lt; ActiveRecord::Base # these two associations say an Account can have many users. # It's also assuming users can be associated with multiple accounts. If that's false # i'd recommend putting the account_id on the user and simply removing this many-to-many table has_many :account_users has_many :users, :through =&gt; :account_users # accounts can be mapped to many projects and projects can be mapped to many accounts # if a project only belongs to one account, drop the accounts_projects many-to-many table # and just put the account_id on the project. has_many :account_projects has_many :projects, :through =&gt; :account_projects end # account_user table will have `id`, `account_id`, `user_id` and anything else you need class AccountUser &lt; ActiveRecord::Base belongs_to :account belongs_to :user end class User &lt; ActiveRecord::Base has_many :projects has_many :files end # account_projects table will have `id`, `account_id`, `project_id` and anything else you need class AccountProject &lt; ActiveRecord::Base belongs_to :account belongs_to :project end class Project &lt; ActiveRecord::Base has_many :data_files has_many :notes has_many :project_users has_many :users end # project_users table will have `id`, `project_id`, `user_id` and anything else you need class ProjectUser &lt; ActiveRecord::Base belongs_to :project belongs_to :user end # data_files table will have `id`, `project_id`, `user_id` and anything else you need class DataFile &lt; ActiveRecord::Base belongs_to :project belongs_to :user end # notes table will have `id`, `project_id`, `user_id` and anything else you need class Note &lt; ActiveRecord::Base belongs_to :project belongs_to :user end </code></pre> <p>Does that help explain how the associations will work in rails?</p> <p><strong>NOTE</strong> The AccountUser, AccountProject and ProjectUser tables are all used for the many to many associations I understood you to need. </p> <p>The through associations are useful if you're ever going to associate any other attributes with the mapping of the associations (something in relation to both the account and user for example). </p> <p>If you just need a simple many-to-many relationship without the need for custom attributes, you can simply use the <code>has_and_belongs_to_many</code> approach, though I usually go for the :through option up front.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload