# Three models problem in 'new' action

**URL:** <https://www.ruby-forum.com/t/three-models-problem-in-new-action/160231>\
**Category:** Rails\
**Created:** [February 13, 2009, 8:51am UTC](https://www.ruby-forum.com/t/three-models-problem-in-new-action/160231 "2009-02-13T08:51:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![josegalvan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@josegalvan](https://www.ruby-forum.com/u/josegalvan)\
**Post date:** [February 13, 2009, 8:51am UTC](https://www.ruby-forum.com/t/three-models-problem-in-new-action/160231/1 "2009-02-13T08:51:34Z")

</div>

Hello,

I have this problem :

When I use three models, and associate them, then, my ‘new’ action need  
more data to work, How can I resolv it, or I do it in the right way ?

Thanks

Jose.

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* my models \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

class Libro \< ActiveRecord::Base  
has\_many :lecciones  
end

class Leccion \< ActiveRecord::Base  
has\_many :vocabularios  
belongs\_to :libro  
end

class Vocabulario \< ActiveRecord::Base  
belongs\_to :leccion  
belongs\_to :libro  
end

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* routes.rb \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ActionController::Routing::Routes.draw do |map|  
map.resources :vocabularios

map.resources :lecciones

map.resources :libros

map.resources :libros, :has\_many =\> :lecciones

map.resources :libros do |libro|  
libro.resources :lecciones do |leccion|  
leccion.resources :vocabularios  
end  
end

map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’  
end

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* my controllers \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

class VocabulariosController \< ApplicationController

def new  
@libro = Libro.find(params[:libro\_id])  
@leccion = @libro.lecciones.find(params[:leccion\_id])  
@vocabulario = @leccion.vocabularios.build(params[:vocabulario])

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @vocabulario }
end

```

end

def create  
@libro = Libro.find(params[:libro\_id])  
@leccion = @libro.lecciones.find(params[:leccion\_id])  
@vocabulario = @leccion.vocabularios.build(params[:vocabulario])

```
# Here '@vocabulario' never have 'libro' and write in the
# database 'NULL'
# why it never works ?

# i need to put something like :

# @vocabulario.libro = @libro

# then the insert in database works fine, WHY ?
# is this ok ?

if @vocabulario.save
  redirect_to libro_leccion_vocabulario_url(@libro, @leccion, 

```

@vocabulario)  
else  
render :action =\> “new”  
end

end

end
