# NewbieHelp

**URL:** <https://www.ruby-forum.com/t/newbiehelp/171958>\
**Category:** Ruby\
**Created:** [July 12, 2009, 11:15am UTC](https://www.ruby-forum.com/t/newbiehelp/171958 "2009-07-12T11:15:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ouikoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e68b1a/32.png) [@ouikoto](https://www.ruby-forum.com/u/ouikoto)\
**Post date:** [July 12, 2009, 11:15am UTC](https://www.ruby-forum.com/t/newbiehelp/171958/1 "2009-07-12T11:15:31Z")

</div>

Im having n error message

ActiveRecord::AssociationTypeMismatch in MovieController#create

Genre expected, got String

Parameters:

{“commit”=\>“Create”,  
“authenticity\_token”=\>“b2aa33146f0a40aa59d887b8e19d0635cbdc2612”,  
“movie”=\>{“title”=\>“Matrix”,  
“price”=\>“15”,  
“description”=\>“sldljsvnsdvdv”,  
“genre”=\>“1”}}

> > view code for movies

# Add new movie
\<%= form\_tag :action =\> 'create' %\>

Title: \<%= text\_field 'movie', 'title' %\>

Price: \<%= text\_field 'movie', 'price' %\>

Genre: \<%= collection\_select(:movie,:genre,@genres,:id,:name) %\>

Description  
\<%= text\_area 'movie', 'description' %\>

\<%= submit\_tag "Create" %\> \<%= link\_to 'Back', {:action =\> 'list'} %\>

> > movie controller

class MovieController \< ApplicationController  
def list  
@movies = Movie.find(:all)  
end

def show  
@movie = Movie.find(params[:id])  
end

def new  
@movie = Movie.new  
@genres = Genre.find(:all)  
end

def create  
@movie = Movie.new(params[:movie])  
if @movie.save  
redirect\_to :action =\> ‘list’  
else  
@genres = Genre.find(:all)  
render :action =\> ‘new’  
end  
end

def edit  
@movie = Movie.find(params[:id])  
@genres = Genre.find(:all)  
end

def update  
@movie = Movie.find(params[:id])  
if @movie.update\_attributes(params[:movie])  
redirect\_to :action =\> ‘show’, :id =\> @movie  
else  
@genres = Genre.find(:all)  
render :action =\> ‘edit’  
end  
end

def delete  
Movie.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end

end

> model for movie

class Movie \< ActiveRecord::Base  
belongs\_to :genre

end

> model for genre

class Genre \< ActiveRecord::Base  
has\_many :movies  
end

> > I’m kind of lost right now. A little help will b highly apprecieated  
> > thanks

---

<div class="post-metadata">

**Author:** ![ouikoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e68b1a/32.png) [@ouikoto](https://www.ruby-forum.com/u/ouikoto)\
**Post date:** [July 12, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/newbiehelp/171958/2 "2009-07-12T12:58:20Z")

</div>

For AR movie.genre is an object of type Genre. Also on creation it  
expects move.genre to be an Object of the type Genre e.g. your Model.  
so the make this work quickly:

@movie = Movie.new(params[:movie])  
@movie.genre = Genre.find :condition =\> { :id =\>  
params[:genre\_id] }, :limit =\> 1

or

You could take a look at nested forms  
[http://ryandaigle.com/articles/2008/7/19/what-s-new-in-edge-rails-nested-models](http://ryandaigle.com/articles/2008/7/19/what-s-new-in-edge-rails-nested-models)
