Arnold Cano
2006-Feb-24 22:30 UTC
[Rails] Problem having two belongs_to of the same class type
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 -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Feb-24 23:35 UTC
[Rails] Re: Problem having two belongs_to of the same class type
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 Cano
2006-Feb-25 15:18 UTC
[Rails] Re: Problem having two belongs_to of the same class type
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 James 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.-- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Feb-25 15:46 UTC
[Rails] Re: Problem having two belongs_to of the same class type
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
2006-Feb-27 17:14 UTC
[Rails] Re: Problem having two belongs_to of the same class type
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 James 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.-- Posted via http://www.ruby-forum.com/.