Assigning alternating attributes

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?).

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs