Topic: a two-dimensional array as a params value

I just finished Ruby on Rails 3 Tutorial.  The final chapter is quite complex.  The tutorial throughout the book basically creates a site where Users can post Microposts.  Also, each User can Follow any other User and then the following User's Microposts will show up on the original User's Micropost feed.

My question has to do with why the create action inside the RelationshipsController has the params variable contain a two-dimensional array.

Here's the code.

User

    class User < ActiveRecord::Base
      attr_accessible :email, :name, :password, :password_confirmation
      has_secure_password
      has_many :microposts, dependent: :destroy
      has_many :relationships, foreign_key: "follower_id", dependent: :destroy
      has_many :followed_users, through: :relationships, source: :followed
      has_many :reverse_relationships, foreign_key: "followed_id", 
          class_name: "Relationship", dependent:   :destroy
      has_many :followers, through: :reverse_relationships, source: :follower
    end

Micropost

    class Micropost < ActiveRecord::Base
      attr_accessible :content
      belongs_to :user
    end

Relationship

    class Relationship < ActiveRecord::Base
      attr_accessible :followed_id

      belongs_to :follower, class_name: "User"
      belongs_to :followed, class_name: "User"
    end

I think this is the code that creates the two-dimensional params variable (but why?)

    <%= form_for(current_user.relationships.build(followed_id: @user.id), remote: true) do |f| %>
      <div><%= f.hidden_field :followed_id %></div>
      <%= f.submit "Follow", class: "btn btn-large btn-primary" %>
    <% end %>

RelationshipsController

    class RelationshipsController < ApplicationController  
      def create
        @user = User.find(params[:relationship][:followed_id])
        current_user.follow!(@user)
        respond_to do |format|
          format.html { redirect_to @user }
          format.js
        end
      end
    end

So maybe I just answered my own question, but I've never seen a two-dimensional array for a params variable.  Can someone maybe shed some light on this?

oh, maybe I should post my routes.rb file as well:

    SampleApp::Application.routes.draw do
      resources :users do
        member do
          get :following, :followers
        end
      end
      resources :sessions, only: [:new, :create, :destroy]
      resources :microposts, only: [:create, :destroy]
      resources :relationships, only: [:create, :destroy]

      root to: 'static_pages#home'
    end

thanks,
mike

Re: a two-dimensional array as a params value

The reason is that when you use form_for and pass it an object/model is places all the fields under a key that represents that object/model.

Since you are passing a relationship object to the form_for it is placing all the fields under the :relationship key.