Metaprogramming Virtual Attributes Discussion
Nice intro into metaprogramming and how concerns can be used.
I'm one of the fresh subscribers and have noticed one thing: no need to quit rails console to clear the console output. To save couple of seconds you can just press cmd + K in terminal (also from rails console) and you're good to continue.
Sweet! I figured there might be something to do that. I only ever clear the terminal in screencasts so I always forget to look that up. :)
I have a model called Customer which belongs to a User. I am trying to implement a way for the Customer to have access to the attributes directly ( e.g.: Customer.first_name
instead of having to write Customer.user.first_name
).
This is what I have so far:
customer.rb
after_initialize :set_user_properties, if: Proc.new { |c| c.user.present? }
def set_user_properties
# binding.pry
c_columns = Customer.columns.map { |c| { c.name.to_sym => c.type } }
u_columns = User.columns.map { |c| { c.name.to_sym => c.type } }.reject { |column| c_columns.include?(column) }.reduce Hash.new, :merge
u_columns.each do |key, value|
attribute key, value, default: self.user.try(key)
end
end