Understanding relations in rails

I have a project in ruby ​​on rails, it consists of bringing the income and expenses of money

I create 3 tables -mov_principal -mov_ingreso -mov_gastos

staying this way

create_table “mov_egresos”, id: :serial, force: :cascade do |t| t.decimal “haber”, precision: 18, scale: 2, null: false t.integer “id_movp” end

create_table “mov_ingreso”, id: :serial, force: :cascade do |t| t.decimal “debe”, precision: 18, scale: 2 t.integer “id_movp” end

create_table “mov_principal”, id: :serial, force: :cascade do |t| t.string “referencia”, limit: 20, null: false end

A record of the main mobile can have an income or can have an expense, never both

If I do a database query (I am using postgresql) it looks like this

Query

this is my models

class MovPrincipal < ActiveRecord::Base self.table_name = ‘mov_principal’

has_one :mov_ingresos, :class_name => 'MovIngreso'
has_one :mov_egresos, :class_name => 'MovEgreso'

end

class MovIngreso < ActiveRecord::Base self.table_name = ‘mov_ingreso’

belongs_to :mov_principal, :class_name => 'MovPrincipal', :foreign_key => :id_movp

end

class MovEgreso < ActiveRecord::Base

belongs_to :mov_principal, :class_name => 'MovPrincipal', :foreign_key => :id_movp

end

I need to enter the income on one page and the expenses on another

I am learning rails and I don’t understand relations

I don’t know if you explain to me, thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs