Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 545195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:44:08+00:00 2026-05-13T10:44:08+00:00

Lets say I have two tables. class CreateUsers < ActiveRecord::Migration def self.up create_table :users

  • 0

Lets say I have two tables.

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string    :type, :default => 'User'
      t.string    :user_name, :null => false
      t.boolean   :is_registered, :default => true
      # ... many more fields
    end
  end
end

class CreateContactInfo < ActiveRecord::Migration
  def self.up
    create_table :contact_info do |t|
      t.integer :resource_id
      t.string :resource_type
      t.string :first_name
      t.string :last_name
      t.string :middle_initial
      t.string :title
    end
  end
end

class ContactInfo < ActiveRecord::Base
  belongs_to :contactable, :polymorphic => true
end

class User < ActiveRecord::Base
  has_one :contact_info, :as => :contactable
  # composed_of :contact_info # ... It would be nice if magics happened here
end

I would like to have the User’s contact_info automatically merged into my User object as attributes of the user object without having to say @user.contact_info.first_name; instead, I would prefer to be able to write @user.first_name.

The reason I am breaking out attributes to the contact_info table is that these are common attributes to multiple models. That is why I am making setting up the contact_info as a polymorphic association.

Does anyone know of a good way to aggregate/merge the attributes of contact_info directly into my user model?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:44:09+00:00Added an answer on May 13, 2026 at 10:44 am

    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.

    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.

    class User < 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 => :contactable
    
      def method_missing(method_id, *args)
        if (!self.respond_to?(method_id) && self.contact_info.respond_to?(method_id))
          self.contact_info.send(method_id, *args)
        elsif (!self.class.respond_to?(method_id) && 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 => :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 << options.reject { |k,v| contact_attrs.include?(k) }.merge(:contact_info => @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
    

    class ContactInfo < ActiveRecord::Base
      set_table_name "contact_info"
    
      belongs_to :contactable, :polymorphic => 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have two tables - child and parent with many-to-one relation. What
Let's say you have two tables, Users and UserRoles. Here's how the two tables
Let's say I have two tables: Report Comment And assuming I have a database
Let's say I have two tables, news and comments. news ( id, subject, body,
Scenario: Let's say I have two tables, TableA and TableB. TableB's primary key is
Let's say I have two existing tables, dogs and cats: dog_name | owner ---------+------
For simplicity lets say I have two flex mxml pages. form.mxml button.mxml If the
Let's say I have two models, Classes and People. A Class might have one
Let's say I have two arrays: int ArrayA[] = {5, 17, 150, 230, 285};
Let's say we have two classes, Foo and Foo Sub, each in a different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.