Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I finally got it! Thank you both amikazmi and Topher Fangio. I had to implement both the delegate and method_missing techniques to get this to work.</p> <p>Here is the total madness that finally ended up working for me! If anybody has suggestions on how to further improve this, I'd love to hear your suggestions.</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible *([:user_name, :udid, :password, :password_confirmation, :contact_info] + ContactInfo.accessible_attributes.to_a.map {|a| a.to_sym}) has_one :contact_info, :as =&gt; :contactable def method_missing(method_id, *args) if (!self.respond_to?(method_id) &amp;&amp; self.contact_info.respond_to?(method_id)) self.contact_info.send(method_id, *args) elsif (!self.class.respond_to?(method_id) &amp;&amp; ContactInfo.respond_to?(method_id)) ContactInfo.send(method_id, *args) else super(method_id, *args) end end # delegating attributes seems redundant with the method_missing above, but this secret sauce works. ContactInfo.accessible_attributes.to_a.each do |a| delegate a.to_sym, "#{a}=".to_sym, :to =&gt; :contact_info end def initialize(*args) options = args.extract_options! contact_attrs = ContactInfo.accessible_attributes.to_a.map{|a| a.to_sym} @ci = ContactInfo.new(options.reject {|k,v| !contact_attrs.include?(k) }) super(*(args &lt;&lt; options.reject { |k,v| contact_attrs.include?(k) }.merge(:contact_info =&gt; @ci) ) ) self.contact_info = @ci end validates_presence_of :user_name validates_uniqueness_of :user_name validates_associated :contact_info def after_save # automatically save the contact info record for the user after the user has been saved. self.contact_info.save! end end </code></pre> <hr> <pre><code>class ContactInfo &lt; ActiveRecord::Base set_table_name "contact_info" belongs_to :contactable, :polymorphic =&gt; true validates_presence_of :email validates_uniqueness_of :email attr_accessible :first_name, :last_name, :middle_initial, :title, :organization_name, :email, :email_2, :twitter_name, :website_url, :address_1, :address_2, :city, :state, :zip, :phone_work, :phone_mobile, :phone_other, :phone_other_type def full_name [self.first_name, self.last_name].compact.join(' ') end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload