title: Rails Many-to-Many Debugging
body: <p>Im writing a rails application for pairing people up when they want to play videogames together. I have a User model, and a Game model. Then I have a UserGame model for the many to many relationship.</p>
  
  <p>The User may choose which games they are willing to play on sign up or in a profile edit page, and their games will be displayed in a (ul) list on their profile page. All that is shown in the list when the page loads, though, is <code>[]</code> and I have no idea what that's supposed to mean...</p>
  
  <p>However, I am finding that after signing up and/or editing the user model, the list appears to remain empty. I am not sure whether I am assigning the fields incorrectly during edit/sign up, or whether I am accessing them incorrectly later for display and honestly I have no idea how to go about finding out... Any help would be appreciated as I am still learning to use both Rails and Ruby. I have been through the Rails tutorial that builds a simple blog, but I am still new to some of the more complicated stuff like this many-to-many.</p>
  
  <p>The User Model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    attr_accessible :name, :password, :password_confirmation
  
    attr_accessor :password
    before_save :encrypt_password
  
    validates_confirmation_of :password
    validates_presence_of :password, :on =&gt; :create
    validates_presence_of :name
    validates_uniqueness_of :name
  
    has_many :user_games
    has_many :games, :through =&gt; :user_games
  
    def self.authenticate(name, password)
      user = find_by_name(name)
      if user &amp;&amp; user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
      else
        nil
      end
    end
  
    def encrypt_password
      if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
      end
    end
  end
  </code></pre>
  
  <p>The Game model:</p>
  
  <pre><code>class Game &lt; ActiveRecord::Base
    attr_accessible :name, :player_count
  
    has_many :user_games
    has_many :users, :through =&gt; :user_games
  end
  </code></pre>
  
  <p>User Update:</p>
  
  <pre><code>def update
    @user = User.authenticate(current_user.name, params[:user][:password])
    respond_to do |format
      if @user
        if @user.update(params[:user])
          format.html{ redirect_to @user, notice: "User was successfully updated!" 
        else
          format.html{ redirect_to edit_profile_path, notice: "Unable to update!" }
        end
      else
          format.html{ redirect_to edit_profile_path, notice: "Unable to authenticate user " + current_user.email }
      end
    end
  end
  </code></pre>
  
  <p>User show.html.erb:</p>
  
  <pre><code>&lt;h1&gt;&lt;%= @user.name %&gt;&lt;/h1&gt;
  
  &lt;p&gt;
    &lt;ul&gt;
    &lt;%= @user.games.each do |g| %&gt;
      &lt;li&gt;&lt;%= g.name %&gt;&lt;/li&gt;
    &lt;% end %&gt;
    &lt;/ul&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>User edit.html.erb:</p>
  
  <pre><code>&lt;%= form_for @user do |f| %&gt;
    &lt;% if @user.errors.any? %&gt;
      &lt;div class="error_messages"&gt;
        &lt;h2&gt;Form is invalid&lt;/h2&gt;
        &lt;ul&gt;
          &lt;% for message in @user.errors.full_messages %&gt;
            &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
    &lt;p&gt;
      &lt;%= f.label :name %&gt;&lt;br /&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :password %&gt;&lt;br /&gt;
      &lt;%= f.password_field :password %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :password_confirmation %&gt;&lt;br /&gt;
      &lt;%= f.password_field :password_confirmation %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label "select any games you're willing to play (you can add new ones later)" %&gt;&lt;br /&gt;
      &lt;%= collection_select( @user, :game_ids, Game.all, :id, :name, {}, { :multiple =&gt; true } ) %&gt;
    &lt;/p&gt;
    &lt;p class="button"&gt;&lt;%= f.submit %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>EDIT: after further attempts to find the problem, I have found that the @user.update() function call is failing. Not sure what could cause this.</p>