Skip to content

Instantly share code, notes, and snippets.

@luizkowalski
Created May 22, 2014 20:38
Show Gist options
  • Save luizkowalski/880cc9e2e4fb7da8b918 to your computer and use it in GitHub Desktop.
Save luizkowalski/880cc9e2e4fb7da8b918 to your computer and use it in GitHub Desktop.
# The classes
class ReducaoZ < ActiveRecord::Base
self.table_name = 'reducaoz'
belongs_to :empresa
belongs_to :impressorafiscal, foreign_key: 'impressorafiscal_id', class_name: 'ImpressoraFiscal'
belongs_to :usuario_cadastro, foreign_key: 'usuariocadastro_id', class_name: 'Usuario'
belongs_to :usuario_alteracao, foreign_key: 'usuarioalteracao_id', class_name: 'Usuario'
has_many :aliquotas, primary_key: 'id', foreign_key: 'reducaoz_id', class_name: 'Aliquota', dependent: :delete_all
end
class Aliquota < ActiveRecord::Base
self.table_name = 'aliquota'
belongs_to :reducaoz, class_name: 'ReducaoZ', primary_key: 'reducaoz_id', foreign_key: 'id'
end
# The method
json_data.aliquotas_list.each_with_index do |al, index|
aliquota = reducao.aliquotas.build
# populate aliquota
red.aliquotas << aliquota
end
red.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment