Creating variables on the fly with a loop


#1

I’m trying to clean up some of my sloppy code, and one of the things I’m
working on is trying to make this huge group of variables a bit more
elegant.

My variables look like this:

@business_core_courses =
Category.find_by_title(‘business_core_courses’).courses

@written_communication =
Category.find_by_title(‘written_communication’).courses

Etc…

It seems like there is a lot of repition going on, and that this could
be cleaned up somehow.

the “business_core_courses” and “written_communication” are both just
values in a field name ‘title’ in the table ‘categories’

I was thinking it would look something along the lines of this:

Categories.title.each do |column|
@category.title = Category.find_by_title(‘category.title’).courses
end

But I’m not too sure what the right syntax to use is.

Any help would be greatly appreciated.