# Assigning alternating attributes

**URL:** <https://www.ruby-forum.com/t/assigning-alternating-attributes/123909>\
**Category:** Rails\
**Created:** [December 21, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/assigning-alternating-attributes/123909 "2007-12-21T22:55:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 21, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/assigning-alternating-attributes/123909/1 "2007-12-21T22:55:04Z")

</div>

I will have a number of teachers each assigned a number of spots on a  
schedule.

The teachers name will be displayed each cell they are assigned and I  
want the background display of each teacher to be a different color  
which I will retrieve in the view and insert into an inline style.

I decided to make the background color an attribute of a teacher\_day  
model. I’ll assign colors shortly after I’ve created a collection of  
teacher\_days for a schedule.

here is a method I tried that doesn’t work

def assign\_colors  
for teacher in @schedule.teacher\_days  
teacher.update\_attributes( :bgcolor =\> cycle(“red”, “green”, “blue”)  
)  
end  
end

The cycle doesn’t seem to work within the update attributes…saying that  
it isnt a method for .

I would like to easily be able to expand or contract the number of items  
in the color array. (I probably should keep it as a constant someplace  
else too?).

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 22, 2007, 12:53am UTC](https://www.ruby-forum.com/t/assigning-alternating-attributes/123909/2 "2007-12-22T00:53:38Z")

</div>

I answered my question using something like this:

def assign\_colors  
color\_array = [“red”, “green”, “blue”]  
count = 0  
for teacher in @schedule.teacher\_days  
cy\_color = color\_array[count]  
count += 1  
count = 0 if count \>= color\_array.length  
teacher.update\_attributes( :bgcolor =\> cy\_color )  
end  
end
