title: Rails build function with 2 Objects
body: <p>I have a problem concerning the build method in rails 3. I want to create a Todo with foreignkeys of project and group.</p>
  
  <p>The associations look like</p>
  
  <p>Group has_many projects and todos</p>
  
  <p>Project has_many todos, belongs_to group</p>
  
  <p>Todo belongs_to 1 project and 1 group</p>
  
  <pre><code>My routes
  resources :groups do
          resources :projects do
              resources :todos
          end
  end
  </code></pre>
  
  <p>work perfect.</p>
  
  <p>The form also works perfectly with:</p>
  
  <pre><code>&lt;%= form_for [@group, @project, @todo] do |f| %&gt;
  </code></pre>
  
  <p>My problem is now the CREATE and UPDATE Method in the Todo-Controller.</p>
  
  <pre><code>   def create
          @group   = Group.find(params[:group_id])
          @project = Project.find(params[:project_id])
          @todo = @group.projects.todos.build(params[:todo])
          @todo.save
     ... 
  </code></pre>
  
  <p>If I write:</p>
  
  <pre><code>@todo = @group.todos.build(params[:todo]) 
  </code></pre>
  
  <p>or
      @todo = @project.todos.build(params[:todo])</p>
  
  <p>the project_id OR group_id is saved in the database. But not with:</p>
  
  <pre><code>@todo = @group.projects.todos.build(params[:todo])
  </code></pre>
  
  <p>My question is now, how I can get access of the group_id and the project_id and save them both into the database.</p>