Forum: Ruby on Rails creating variables on the fly with a loop

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7813d26c8ef483e08432ec9c574664d6?d=identicon&s=25 Ben (Guest)
on 2006-05-11 19:29
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.
This topic is locked and can not be replied to.