All threads / Metaprogramming Virtual Attributes Discussion
Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

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

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.