Skip to content

Instantly share code, notes, and snippets.

@kaochenlong
Created July 6, 2016 02:14
Show Gist options
  • Save kaochenlong/88719a8fde9914c6ea82c83991a368e6 to your computer and use it in GitHub Desktop.
Save kaochenlong/88719a8fde9914c6ea82c83991a368e6 to your computer and use it in GitHub Desktop.
module ActiveRecord
class Base
def self.has_one(something)
define_method "#{something}=".to_sym do |new_value|
instance_variable_set("@#{something}", new_value)
end
define_method something.to_sym do
instance_variable_get("@#{something}")
end
end
end
end
class Product < ActiveRecord::Base
has_one :user
end
p1 = Product.new
p1.user = "123"
puts p1.user #=> 123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment