Delegate id should give nil when allow_nil #3557

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants
Contributor

ka8725 commented Nov 7, 2011

When I delegete id and pass allow_nil as true I should get nil according documentation.

I have polymorphic associations for customer: belongs_to :shipping_location, belongs_to :billing_location (default locations for customer) and has_many :locations:

class Customer < ActiveRecord::Base
      has_one :billing_location, :as => :locationable, :class_name => 'Location', :conditions => { :default_billing => true }
      has_one :shipping_location, :as => :locationable, :class_name => 'Location', :conditions => { :default_shipping => true }
      has_many :locations, :as => :location able
      with_options(:prefix => true, :allow_nil => true) do |options|
          options.delegate :id, :to => :shipping_location, :prefix => true, :allow_nil => true
          options.delegate :id, :to => :billing_location, :prefix => true, :allow_nil => true
      end
end

I want to fill in the form like this to set default locations:

<% fields_for @customer do |fc| %>
    <%= fc.label "#{location_type}_location_id", 'Saved Address', {:id => nil} %>
    <%= fc.collection_select "#{location_type}_location_id", @customer.locations, :id, :template_name,
        {:include_blank => 'Please choose...'},
        {:id => "saved-#{location_type}-location", :class => 'selectbox', :rel => customer_location_template_path(':id')} %>
<% end %>

where location_type is either 'shipping' or 'billing'. But I have raise RuntimeError instead of get nil when shipping_location is nil or billing_location is nil

@ka8725 ka8725 Delegate id with prefix and allow_nil should give nil
When I delegete id to object with prefix and allow_nil I should get nil according documentation
c99649e
Member

jonleighton commented Nov 16, 2011

Hi,

I am not sure it's a bug. If you have nil on Ruby 1.8, it has an id method, so will get called successfully. It's debatable whether that's the best implementation, but we can't change it now.

Also, patches should be submitted to the master branch.

Thanks

I'd say the likelihood of one being interested in nil's id is very low. Seems the expected behavior to me. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment