# Problem having two belongs\_to of the same class type

**URL:** <https://www.ruby-forum.com/t/problem-having-two-belongs-to-of-the-same-class-type/54168>\
**Category:** Rails\
**Created:** [February 24, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/problem-having-two-belongs-to-of-the-same-class-type/54168 "2006-02-24T23:29:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnoldcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@arnoldcano](https://www.ruby-forum.com/u/arnoldcano)\
**Post date:** [February 24, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/problem-having-two-belongs-to-of-the-same-class-type/54168/1 "2006-02-24T23:29:59Z")

</div>

I have a Shipment class that is used to ship packages between Branches.  
However, when I try to have two belongs\_to in Shipment called shipped\_to  
and shipped\_from I can’t seem to make it work. I can get it to display  
the form properly but when I select the branches and click save I get  
the following error…

“Branch expected, got String”

Any ideas? I have added some code snipplets below for reference.

Thanks!

## shipment.rb:

class Shipment \< ActiveRecord::Base  
belongs\_to :shipped\_to,  
:class\_name =\> ‘Branch’,  
:foreign\_key =\> ‘shipped\_to\_id’  
belongs\_to :shipped\_from,  
:class\_name =\> ‘Branch’,  
:foreign\_key =\> ‘shipped\_from\_id’  
end

## branch.rb

class Branch \< Entity  
has\_many :shipped\_to,  
:class\_name =\> ‘Shipment’,  
:order =\> ‘updated\_on DESC’,  
:foreign\_key =\> ‘shipped\_to\_id’,  
:dependent =\> true  
has\_many :shipped\_from,  
:class\_name =\> ‘Shipment’,  
:order =\> ‘updated\_on DESC’,  
:foreign\_key =\> ‘shipped\_from\_id’,  
:dependent =\> true  
end

## entity.rb

class Entity \< ActiveRecord::Base  
end

## \_form.rhtml:

\<%= error\_messages\_for ‘shipment’ %\>

| Shipped to | \<%= collection\_select 'shipment', 'shipped\_to', @branches, 'id', 'name' %\> |
| Shipped from | \<%= collection\_select 'shipment', 'shipped\_from', @branches, 'id', 'name' %\> |
|
| |

## shipments\_controller.rb:

class ShipmentsController \< ApplicationController  
def new  
@branches = Branch.find(:all, :order =\> ‘name’)  
case request.method  
when :get  
@shipment = Shipment.new  
when :post  
@shipment = Shipment.new(params[:shipment])  
if @shipment.save  
flash[:notice] = ‘Shipment was successfully created’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![arnoldcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@arnoldcano](https://www.ruby-forum.com/u/arnoldcano)\
**Post date:** [February 25, 2006, 12:38am UTC](https://www.ruby-forum.com/t/problem-having-two-belongs-to-of-the-same-class-type/54168/2 "2006-02-25T00:38:15Z")

</div>

Arnold Cano wrote:

> “Branch expected, got String”  
> …
> 
> \<%= collection\_select ‘shipment’, ‘shipped\_to’, @branches, ‘id’, ‘name’ %\> Shipped from \<%= collection\_select 'shipment', 'shipped\_from', @branches, 'id', 'name' %\>
> 
> Use shipped\_to\_id and shipped\_from\_id in your collection\_selects.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![arnoldcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@arnoldcano](https://www.ruby-forum.com/u/arnoldcano)\
**Post date:** [February 25, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/problem-having-two-belongs-to-of-the-same-class-type/54168/3 "2006-02-25T16:18:16Z")

</div>

I updated my form with your suggestion as follows:

| Shipped to | \<%= collection\_select 'shipment', 'shipped\_to\_id', @branches, 'id', 'name' %\> |
| Shipped from | \<%= collection\_select 'shipment', 'shipped\_from\_id', @branches, 'id', 'name' %\> |

But now when I click save I get the following error…

“interning empty string”

Any other ideas? I’m stumped!

Mark Reginald J. wrote:

> Arnold Cano wrote:
> 
> > “Branch expected, got String”  
> > …
> > 
> > \<%= collection\_select ‘shipment’, ‘shipped\_to’, @branches, ‘id’, ‘name’ %\> Shipped from \<%= collection\_select 'shipment', 'shipped\_from', @branches, 'id', 'name' %\>
> > 
> > Use shipped\_to\_id and shipped\_from\_id in your collection\_selects.
> > 
> > –  
> > We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![arnoldcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@arnoldcano](https://www.ruby-forum.com/u/arnoldcano)\
**Post date:** [February 27, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/problem-having-two-belongs-to-of-the-same-class-type/54168/4 "2006-02-27T18:13:30Z")

</div>

You were absolutely right I had a bug in my validation code that was  
causing this error. I just thought it had something to do with the  
belongs\_to because it kept pointing me to the line number where the  
actual save was taking place.

Thanks for your help!

Arnold

Mark Reginald J. wrote:

> Arnold Cano wrote:

> > But now when I click save I get the following error…
> > 
> > “interning empty string”
> > 
> > Any other ideas? I’m stumped!
> 
> That’s some other problem. Have a look at the full stack trace  
> of the error to work out where it is occuring.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![arnoldcano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@arnoldcano](https://www.ruby-forum.com/u/arnoldcano)\
**Post date:** [February 25, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/problem-having-two-belongs-to-of-the-same-class-type/54168/5 "2006-02-25T16:47:37Z")

</div>

Arnold Cano wrote:

> But now when I click save I get the following error…
> 
> “interning empty string”
> 
> Any other ideas? I’m stumped!

That’s some other problem. Have a look at the full stack trace  
of the error to work out where it is occuring.

–  
We develop, watch us RoR, in numbers too big to ignore.
