# Undefined method 'each'

**URL:** <https://www.ruby-forum.com/t/undefined-method-each/153475>\
**Category:** Ruby\
**Created:** [November 17, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/undefined-method-each/153475 "2008-11-17T19:41:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [November 17, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/undefined-method-each/153475/1 "2008-11-17T19:41:46Z")

</div>

I am following along in Ben Scofield’s book (Practical REST on Rails 2  
Projects). As I was building the MovieList application, I ran into an  
undefined method for each, which I can’t figure out how to debug (I am  
new to ruby/rails).

Can someone point me in the correct direction on how to debug this  
issue?

NoMethodError in MoviesController#update

undefined method `each’ for #Role:0x56ea30c

RAILS\_ROOT: E:/study/rails/movielist

Application Trace[http://localhost:3000/movies/1](http://localhost:3000/movies/1) | Framework  
Trace[http://localhost:3000/movies/1](http://localhost:3000/movies/1) | Full  
Trace[http://localhost:3000/movies/1](http://localhost:3000/movies/1)

E:/bin/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active\_record/attribute\_methods.rb:256:in  
`method\_missing’

app/models/movie.rb:17:in `deleted\_roles=’

app/controllers/movies\_controller.rb:70:in `update’

app/controllers/movies\_controller.rb:69:in `update’

movies\_controller.rb:

65 def update

66 @movie = Movie.find(params[:id])

67 debugger

68

69 respond\_to do |format|

70 if @movie.update\_attributes(params[:movie])

71 flash[:notice] = ‘Movie was successfully updated.’

72 format.html { redirect\_to(@movie) }

73 format.xml { head :ok }

74 else

75 format.html { render :action =\> “edit” }

76 format.xml { render :xml =\> @movie.errors, :status =\>  
:unprocessable\_entity }

77 end

78 end

79 end

Movies.rb:

16 def deleted\_roles=(values)

17 roles.find(\*values).each(&:destroy)

18 end

Role.rb:

class Role \< ActiveRecord::Base  
belongs\_to :movie  
belongs\_to :person

validates\_presence\_of :movie\_id, :person\_id, :name

def to\_s  
[person.full\_name, name].join(’ - ')  
end  
end

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [November 17, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/undefined-method-each/153475/2 "2008-11-17T20:11:08Z")

</div>

Hi Joshua,

Joshua B. wrote:

> NoMethodError in MoviesController#update
> 
> undefined method `each’ for #Role:0x56ea30c

You should post this question to the Rails list at  
removed\_email\_address@domain.invalid When you do, you’ll need to provide  
some  
additional info.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [November 17, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/undefined-method-each/153475/3 "2008-11-17T20:12:54Z")

</div>

Hi.

Note that #deleted\_roles= is a setter method (you can tell by the  
equality  
sign) and that in the update method you are assigning to this method.

You probably need to look into attr\_accessible to make a whitelist of  
all  
setters you want to be mass-assignable during #update and #create on  
your  
controller (a mass-assignable attribute is one that you can safely  
assign  
values to without worry – an example of something you wouldn’t want to  
mass  
assign is an the primary key value).

James
