Note that there are some explanatory texts on larger screens.

plurals
  1. POhas_many :through questions
    primarykey
    data
    text
    <p>I was previously using has_and_belongs_to_many, and have converted to has_many :through. Here's how it looks for a list of games that can have many users playing. With this, I can do game.users and user.games....:</p> <pre><code>class Game &lt; ActiveRecord::Base has_many :game_users, :dependent =&gt; :destroy has_many :users, :through =&gt; :game_users, :uniq =&gt; true end class User &lt; ActiveRecord::Base has_many :game_users, :dependent =&gt; :destroy has_many :games, :through =&gt; :game_users, :uniq =&gt; true end class GameUser &lt; ActiveRecord::Base belongs_to :game belongs_to :user end </code></pre> <p>And my database migration for the join table:</p> <pre><code>create_table :game_users, :id =&gt; false do |t| t.column :game_id, :integer t.column :user_id, :integer t.column :player_index, :integer end </code></pre> <p>I'm not quite sure I get all this, please help me check my facts:</p> <ol> <li><p>Is the dependent => :destroy correct? I want the 'game_users' join table entry to be deleted if either the game or the user is destroyed - but I don't want users to be deleted if games are deleted and vice versa..... ?</p></li> <li><p>The uniq field is supposed to say that games contain only unique users, and the users only contain unique games. Is that correct?</p></li> <li><p>The database migration as before has :id => false. Is that still the right thing to do? I tried in the console destroying a game, and got complaints about the missing id... so I'm guessing not and trying to understand why.</p></li> </ol> <p>I find rails active record associations very confusing. I guess they aren't supposed to be!</p>
    singulars
    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.
 

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