Module: Microframe::ORM::Relationships

Included in:
Base
Defined in:
lib/microframe/orm/relationships.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(model, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/microframe/orm/relationships.rb', line 14

def belongs_to(model, options = {})
  define_method(model) do
    options[:foreign_key] ||= "id"
    model_id = send("#{model}_id")
    Module.const_get(model.to_s.capitalize).where(options[:foreign_key] => model_id ).fetch.first
  end
end

#has_many(model, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/microframe/orm/relationships.rb', line 5

def has_many(model, options = {})
  model = model.to_s
  define_method(model) do
    model_class = model[0..-2]
    options[:foreign_key] ||= self.class.to_s.downcase + "_id"
    Module.const_get(model_class.capitalize).where(options[:foreign_key] => id )
  end
end