views:

12

answers:

1

I have three Models (and growing): ContactEmail, ContactCall, ContactPostalcard

I want to cycle through the three of them to go through a pretty lengthy loop.

A sample would would be the following:

import_event = ContactEmail.sugarcrm_is(false) #using searchlogic gem

The second loop would be:

import_event = ContactCall.sugarcrm_is(false)

I would I guess like a way to do something like:

event_array = ["ContactEmail", "ContactCall", "ContactPostalcard"]

event_array.each do |event|
  import_event = event_array.sugarcrm_is(false)
  .....

end

But not sure how to do that...thanks!

+2  A: 

There are multiple ways to attack it, but the easiest is probably the following:

event_array = ["ContactEmail", "ContactCall", "ContactPostalcard"]
event_array.each do |event|
  import_event = event.constantize.sugarcrm_is(false)
  ...
end

constantize converts the string into a constant. Then you can make the class calls. It's a rails helper function.

You could also just have the array contain the classes instead of strings:

event_array = [ContactEmail, ContactCall, ContactPostalcard]
event_array.each do |event|
  import_event = event.send(:sugarcrm_is, false)
  ...
end

It's a little cleaner, but it all comes down to preference.

Tony Fontenot
cool, yes, I like the second way better if it works...thanks will check it out.
Angela
sweet, this looks like it is it...thanks +1
Angela