Method: Associatable#belongs_to

Defined in:
lib/easy_save/associatable.rb

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/easy_save/associatable.rb', line 51

def belongs_to(name, options = {})
  options = BelongsToOptions.new(name.to_s, options)
  self.assoc_options[name] = options

  define_method(name) do
    fk_method = options.send(:foreign_key)
    fk_id = self.send(fk_method)

    class_name = options.class_name.constantize

    res = class_name.where(id: fk_id)
    res.first || nil
  end
end