New Discussion

Notifications

You’re not receiving notifications from this thread.

Metaprogramming Virtual Attributes Discussion

3
General

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. :)

One suggestion. Post references about related screencasts, gems etc in description please)

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
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 88,440+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.