module Raamen::Associatable

Public Instance Methods

assoc_options() click to toggle source
# File lib/raamen/sql_object_modules/associatable.rb, line 86
def assoc_options
  @assoc_options ||= {}
end
belongs_to(name, options = {}) click to toggle source
# File lib/raamen/sql_object_modules/associatable.rb, line 35
def belongs_to(name, options = {})
  self.assoc_options[name] = BelongsToOptions.new(name, options)

  define_method(name) do
    options = self.class.assoc_options[name]
    key_val = self.send(options.foreign_key)
    options.model_class.where(options.primary_key => key_val).first
  end
end
has_many(name, options = {}) click to toggle source
# File lib/raamen/sql_object_modules/associatable.rb, line 45
def has_many(name, options = {})
  self.assoc_options[name] = HasManyOptions.new(name, self.name, options)

  define_method(name) do
    options = self.class.assoc_options[name]
    key_val = self.send(options.primary_key)
    options.model_class.where(options.foreign_key => key_val)
  end
end
has_one_through(name, through_name, source_name) click to toggle source
# File lib/raamen/sql_object_modules/associatable.rb, line 55
    def has_one_through(name, through_name, source_name)
      define_method(name) do
        through_options = self.class.assoc_options[through_name]
        source_options = through_options.model_class.assoc_options[source_name]

        through_table = through_options.table_name
        through_pk = through_options.primary_key
        through_fk = through_options.foreign_key

        source_table = source_options.table_name
        source_pk = source_options.primary_key
        source_fk = source_options.foreign_key

        key_val = self.send(through_fk)
        results = DBConnection.execute(<<-SQL, key_val)
          SELECT
            #{source_table}.*
          FROM
            #{through_table}
          JOIN
            #{source_table}
          ON
            #{through_table}.#{source_fk} = #{source_table}.#{source_pk}
          WHERE
            #{through_table}.#{through_pk} = ?
        SQL

        source_options.model_class.parse_all(results).first
      end
    end