English 中文(简体)
RoR: How to Change fields on Join statement generated by Active Record s sum function
原标题:

I m doing a sum using the Sum function provided by RubyOnRails Active Record as follows:

s=DatosMateria.sum( inscritos ,:conditions=> "datos_materia.ano=2005 AND materias.codigo=2394",:include=>"materias")

it returns 0 and generates me the following SQL statement:

SELECT sum( datos_materia .inscritos) AS  sum_inscritos FROM  datos_materia  LEFT OUTER JOIN  materias  ON materias.codigo=datos_materia.id WHERE..

I need it to join on fields materias.codigo=datos_materia.materia_codigo instead of materias.codigo=datos_materia.id (materias.codigo and datos_materia.id are both primary keys and they re not the same type nor associated keys.)

The involved models are shown below:

 
class DatosMateria < ActiveRecord::Base
    set_table_name  datos_materia 
    has_many :materias,:foreign_key =>  codigo 
end

class Materia < ActiveRecord::Base set_primary_key codigo belongs_to :datosMateria, :foreign_key=> materia_codigo end

最佳回答

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 _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.

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签