Forum: Ruby on Rails Problem having two belongs_to of the same class type

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.
Arnold C. (Guest)
on 2006-02-25 00:29
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' %>

<table>
  <tr>
    <th>Shipped to</th>
    <td><%= collection_select 'shipment', 'shipped_to', @branches, 'id',
'name' %></td>
  </tr>
  <tr>
    <th>Shipped from</th>
    <td><%= collection_select 'shipment', 'shipped_from', @branches,
'id', 'name' %></td>
  </tr>
  <tr>
</table>

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
Mark Reginald J. (Guest)
on 2006-02-25 01:38
(Received via mailing list)
Arnold Cano wrote:

> "Branch expected, got String"
> ...
>     <td><%= collection_select 'shipment', 'shipped_to', @branches, 'id', 'name' %></td>
>   </tr>
>   <tr>
>     <th>Shipped from</th>
>     <td><%= collection_select 'shipment', 'shipped_from', @branches, 'id', 'name' 
%></td>

Use shipped_to_id and shipped_from_id in your collection_selects.

--
We develop, watch us RoR, in numbers too big to ignore.
Arnold C. (Guest)
on 2006-02-25 17:18
I updated my form with your suggestion as follows:

<table>
  <tr>
    <th>Shipped to</th>
    <td><%= collection_select 'shipment', 'shipped_to_id', @branches,
'id', 'name' %></td>
  </tr>
  <tr>
    <th>Shipped from</th>
    <td><%= collection_select 'shipment', 'shipped_from_id', @branches,
'id', 'name' %></td>
  </tr>
</table>

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"
>> ...
>>     <td><%= collection_select 'shipment', 'shipped_to', @branches, 'id', 'name' %></td>
>>   </tr>
>>   <tr>
>>     <th>Shipped from</th>
>>     <td><%= collection_select 'shipment', 'shipped_from', @branches, 'id', 'name' 
%></td>
>
> Use shipped_to_id and shipped_from_id in your collection_selects.
>
> --
> We develop, watch us RoR, in numbers too big to ignore.
Mark Reginald J. (Guest)
on 2006-02-25 17:47
(Received via mailing list)
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.
Arnold Cano (Guest)
on 2006-02-27 19:13
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.
This topic is locked and can not be replied to.