Module: BelongsToResource
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/belongs_to_resource.rb
Overview
Atua como se fosse um belongs_to do active_record, mas ao invés de manter um relacionamento com um model do active_record, mantem um relacionamento com um model do active_resource.
Ex:
class BudgetStructure < ActiveResource::Base
...
end
class Contract < ActiveRecord::Base
include BelongsToResource
belongs_to_resource :budget_structure
belongs_to_resource :budget_allocation
private
def budget_allocation_params
{ includes: :expense_nature, methods: :amount }
end
end
c = Contract.last
c.budget_structure_id
> 3
c.buget_allocation.expense_nature.id
> 1
c.buget_allocation.amount
> 500.0
c.budget_structure
> consulta faz find na API
> <#BudgetStructure teste>
c.budget_structure = BudgetStucture.last c.budget_structure
> não precisa fazer consulta na API e já atualiza o id
> <#BudgetStructure novo>
c.budget_structure(false)
> não usa o cache, ou seja força o find na API
> <#BudgetStructure novo>
c.budget_structure_id = 34 c.budget_structure
> vai fazer a consulta na API pra atualizar o cache, porque mudou o id
> <#BudgetStructure outro>
Defined Under Namespace
Modules: ClassMethods