Created
January 25, 2016 23:11
-
-
Save bryanwoods/73461aa0b92c795c475c to your computer and use it in GitHub Desktop.
This file contains 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
require 'active_support/core_ext/string' | |
class MyFakeActiveRecord | |
def pluralized_table | |
self.class.name.underscore.downcase.pluralize | |
end | |
def self.has_many(association) | |
define_method(association) do | |
"INNER JOIN #{association} ON #{association}.id = #{pluralized_table}.id" | |
end | |
end | |
def self.belongs_to(association) | |
define_method(association) do | |
"INNER JOIN #{association.to_s.pluralize} ON #{association}.id = #{pluralized_table}.id" | |
end | |
end | |
end | |
class SeaFoodShop < MyFakeActiveRecord | |
has_many :octopi | |
end | |
class Octopus < MyFakeActiveRecord | |
belongs_to :sea_food_shop | |
end | |
sea_food_shop = SeaFoodShop.new | |
puts sea_food_shop.octopi | |
octopus = Octopus.new | |
puts octopus.sea_food_shop | |
__END__ | |
INNER JOIN octopi ON octopi.id = sea_food_shops.id | |
INNER JOIN sea_food_shops ON sea_food_shop.id = octopi.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment