module Rollerskates::Associable

Public Instance Methods

belongs_to(table) click to toggle source
# File lib/rollerskates/orm/associable.rb, line 3
def belongs_to(table)
  parent_model = table.to_s.camelize.constantize
  define_method(table) do
    parent_model.find_by(id: send("#{table}_id"))
  end
end
has_many(table) click to toggle source
# File lib/rollerskates/orm/associable.rb, line 10
def has_many(table)
  child_model = table.to_s.camelize.constantize
  child_table = table.to_s.pluralize
  parent_model = model_name.to_s.downcase
  define_method(child_table) do
    column = "#{parent_model}_id"
    child_model.where(column => id)
  end
end