A: 

Edit: After reworking this post I've come to the conclusion that your relationship between Materia and DatosMateria are wrong. It looks like you've got belongs_to and has_many backward. You mention that materias.codigo and datos_materia.id are both primary keys. That you want to join on the materia primary key implies that materia should have many datos_materia, and not the other way around as your assocations are defined.

In short: the description of your associations and problem, it looks like were trying to save the primary key of the one side of a one one to many relation ship as a foreign key on the many side. This is not how relational databases work. The way they usually go is the many side of a one to many relationship will store the primary key of the associated record as a foreign key.

It looks like a few things have gotten lost in translation. So here's a better explanation of what's wrong with your relationships.

Rails expects foreign keys to be named "#{foreign_class}_id", and will store the id associated foreign class. The foreign key is always found on the belongs_to side of things.

With the relationships defined in the question.

All association helper methods called on DatosMateria or an instance of it will join on datos_materia.id = materias.codigo.

Where the foreig\n_key is expected to be materias.codigo. But materias.codigo the primary key, so how can a materia be linked to a datos_materia?

@datos_materia.materias.create

Will create a new Materia record with @datos_materia.id stored in the Materia's codigo's column. Where as establishing the relationship the other way around will create the new materia with @datos_materia.materia_codigo stored in the materia's codigo column.

Materia.create(:datos_materia => @datos_materia)

Assocation helper methods called on Materia or an instance of it will join on datos_materia.id = materias.materia_codgio

There is no simple fix. You will need to redefine your models and completely rework your tables. Assuming you were looking to set up that one materia has many datos_materia. Here are the correctly defined relationships you were going for:

class DatosMateria < ActiveRecord::Base
  set_table_name 'datos_materia'
  belongs_to :materias, :foreign_key => :materia_codigo
end

class Materia < ActiveRecord::Base
  has_many :datos_materias, :foreign_key => :materia_codigo
end

However one you've defined your relationship properly your Sum will work as it should.

EmFi
Thanks for your response, the field materia_codigo is not the primary key but the associated field to 'codigo' in my Materia Model. I tried your answer and didn't worked out for me.
rhernandez
I glossed over some details on account of the language used. I've found the root of your problem and updated my answer. But it's not good news.
EmFi
Thank you EmFi. As you said it I had the association backwards. I rewrote the models just like you put it with some details: belongs_to :materia without the S, has_many :datosMaterias,and finally, adding the set_primary_key 'codigo' in Materia Model. Thank you again.
rhernandez