Created
November 12, 2009 23:39
-
-
Save avit/233431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductData < ActiveRecord::Base | |
belongs_to :product | |
def self.attribute_column_names | |
return @@attr_columns if defined?(@@attr_columns) | |
readers = content_columns.map { |n| n.name.intern } - [:created_at,:updated_at] | |
@@attr_columns ||= readers.map { |k| [k, "#{k}=".to_sym] }.flatten | |
end | |
end | |
class Product < ActiveRecord::Base | |
has_one :product_data | |
accepts_nested_attributes_for :product_data | |
delegate( *ProductData.attribute_column_names << {:to => :product} ) | |
def initialize(*args) | |
super(*args) | |
build_product_data if product_data.nil? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Address < ActiveRecord::Base | |
belongs_to :addressable, :polymorphic => true | |
def self.attribute_column_names | |
return @@attr_columns if defined?(@@attr_columns) | |
readers = content_columns.map { |n| n.name.intern } - [:addressable_type,:created_at,:updated_at] | |
@@attr_columns ||= readers.map { |k| [k, "#{k}=".to_sym] }.flatten | |
end | |
end | |
class User < ActiveRecord::Base | |
has_one :address, :as => :addressable, :dependent => :destroy | |
accepts_nested_attributes_for :address | |
delegate( *Address.attribute_column_names << {:to => :address, :prefix => :address} ) | |
end | |
#>> u = User.new | |
#=> #<User id: nil, store_id: nil, login: nil, email: nil, name: nil, role: "guest", state: "pending", group_id: nil, crypted_password: nil, password_salt: nil, persistence_token: nil, perishable_token: nil, login_count: nil, last_request_at: nil, last_login_at: nil, current_login_at: nil, last_login_ip: nil, current_login_ip: nil, created_at: nil, updated_at: nil, phone: nil> | |
#>> u.address.is_a? Address | |
#=> true | |
#>> u.methods.select { |m| m =~ /address/ } | |
#=> ["address", "address=", "loaded_address?", "set_address_target", "create_address", "has_one_dependent_destroy_for_address", "build_address", "autosave_associated_records_for_address", "validate_associated_records_for_address", "address_addressee", "address_addressee=", "address_attributes=", "address_line_1", "address_line_1=", "address_line_2", "address_line_2=", "address_city", "address_city=", "address_region", "address_region=", "address_country", "address_country=", "name_with_email_address", "address_postal_code", "address_postal_code="] | |
#>> u.address_line_1 | |
#=> nil | |
#>> u.address_line_1 = "123 Main" | |
#=> "123 Main" | |
#>> u.address.line_1 | |
#=> "123 Main" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment