# Proper use of "send" method

**URL:** <https://www.ruby-forum.com/t/proper-use-of-send-method/85295>\
**Category:** Rails\
**Created:** [February 12, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/proper-use-of-send-method/85295 "2007-02-12T13:07:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thushan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@Thushan](https://www.ruby-forum.com/u/Thushan)\
**Post date:** [February 12, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/proper-use-of-send-method/85295/1 "2007-02-12T13:07:52Z")

</div>

I have the following models:

class MainService \< ActiveRecord::Base  
has\_one :travel\_service  
end

class TravelService \< ActiveRecord::Base  
belongs\_to :main\_service  
end

I can use the following:

@main\_service.travel\_service = TravelService.new

But I can’t use the following:  
@main\_service.send(“travel\_service”) = TravelService.new

It says " unexpected ‘=’, expecting $ "

What’s the correct way of using the “send” method?  
(I think this is mainly due to my bad ruby knowledge ☹ )

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![Thushan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@Thushan](https://www.ruby-forum.com/u/Thushan)\
**Post date:** [February 12, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/proper-use-of-send-method/85295/2 "2007-02-12T13:16:54Z")

</div>

On 2/12/07, Thushan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But I can’t use the following:  
> @main\_service.send(“travel\_service”) = TravelService.new
> 
> It says " unexpected ‘=’, expecting $ "

Hi Thushan,

Try:

@main\_service.send(“travel\_service=”, TravelService.new)

#send is a plain old method, and can’t be called on the LHS of an  
assignment. The method you want to send is “travel\_service=”, with  
TravelService.new as an argument.

> Thanks in advance for any help.

'Welcome!  
George.

---

<div class="post-metadata">

**Author:** ![Thushan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@Thushan](https://www.ruby-forum.com/u/Thushan)\
**Post date:** [February 12, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/proper-use-of-send-method/85295/3 "2007-02-12T15:57:18Z")

</div>

Thanks (again) George. That worked.

---

<div class="post-metadata">

**Author:** ![Thushan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@Thushan](https://www.ruby-forum.com/u/Thushan)\
**Post date:** [February 12, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/proper-use-of-send-method/85295/4 "2007-02-12T18:41:27Z")

</div>

George O. wrote:

> Hi Thushan,
> 
> Try:
> 
> @main\_service.send(“travel\_service=”, TravelService.new)

That’s nice and all, but why on earth would you do this?

@main\_service.travel\_service = TravelService.new

seems much much simpler.

---

<div class="post-metadata">

**Author:** ![Thushan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@Thushan](https://www.ruby-forum.com/u/Thushan)\
**Post date:** [February 13, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/proper-use-of-send-method/85295/5 "2007-02-13T17:41:21Z")

</div>

Actually, what I want is this:

@main\_service.send(service\_type, TravelService.new)

Where service\_type may contain sevral other service types (including  
travel\_service). All those service types belongs\_to MainService (and  
MainService has\_one of each of them).

On Feb 12, 10:41 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
