chell
chell

Reputation: 7866

how to use object attribute value for method name ruby

I have a two mailers

welcome_manger(user)  welcome_participant(user)

Both send different information and have different layouts.

when I call the deliver method I would like to use something like the following

UserMailer.welcome_self.role(self.user)

This does not work. How can I accomplish this?

Upvotes: 0

Views: 67

Answers (1)

mu is too short
mu is too short

Reputation: 434635

Something like this perhaps:

m = 'welcome_' + self.role
UserMailer.send(m.to_sym, [self.user])

Assuming that self.role returns a String.

The send method invokes a method by name:

obj.send(symbol [, args...]) → obj
Invokes the method identified by symbol, passing it any arguments specified.

So you just need to build the appropriate method name as a string and then convert it a symbol with to_sym.

Upvotes: 2

Related Questions