Module: RailzLite::Associatable
- Included in:
- SQLObject
- Defined in:
- lib/railz_lite/models/associatable.rb
Instance Method Summary collapse
- #assoc_options ⇒ Object
- #belongs_to(name, options = {}) ⇒ Object
- #has_many(name, options = {}) ⇒ Object
Instance Method Details
#assoc_options ⇒ Object
72 73 74 75 |
# File 'lib/railz_lite/models/associatable.rb', line 72 def @assoc_options ||= {} @assoc_options end |
#belongs_to(name, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/railz_lite/models/associatable.rb', line 51 def belongs_to(name, = {}) = BelongsToOptions.new(name, ) [name] = define_method(name) do foreign_key = send(.foreign_key) primary_key = .primary_key params = [[primary_key, foreign_key]].to_h .model_class.where(params).first end end |
#has_many(name, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/railz_lite/models/associatable.rb', line 62 def has_many(name, = {}) = HasManyOptions.new(name, self.name, ) define_method(name) do foreign_key = .foreign_key primary_key = send(.primary_key) params = [[foreign_key, primary_key]].to_h .model_class.where(params) end end |