Skip to content

Instantly share code, notes, and snippets.

@bryanwoods
Created January 25, 2016 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanwoods/73461aa0b92c795c475c to your computer and use it in GitHub Desktop.
Save bryanwoods/73461aa0b92c795c475c to your computer and use it in GitHub Desktop.
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