Module: Rollerskates::Associable
- Included in:
- BaseModel
- Defined in:
- lib/rollerskates/orm/associable.rb
Instance Method Summary collapse
Instance Method Details
#belongs_to(table) ⇒ Object
3 4 5 6 7 8 |
# 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) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# 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 |