Dropdown list - belongs_to_association

324 views
Skip to first unread message

ejmm320

unread,
Feb 14, 2012, 2:40:30 PM2/14/12
to rails_admin
Hi all,

I have a problem with belongs_to association when the foreign_key
isn't the default 'model_id'.

I can show associations properly in list view but when i try to add
new records it fails because the 'value' attribute in dropdown select
by default is the model_id instead of my associated field:

class Tc::TcAgencia < ActiveRecord::Base
belongs_to :tc_tipo_agencia, :class_name
=>"Tc::TcTipoAgencia", :foreign_key =>
"tc_tipo_agencia_codigo", :primary_key =>"codigo"
belongs_to :tc_estado
belongs_to :tc_region, :class_name =>"Tc::TcRegion", :foreign_key =>
"tc_region_codigo", :primary_key =>"codigo"
validates :codigo, :uniqueness => true

def mostrar_nombre_agencia
"#{self.nombre}"
end
end

in rails_admin.rb

config.model Tc::TcAgencia do
label 'Agencias'
field :id, :integer
field :codigo, :string
field :nombre, :string
field :tc_estado
field :tc_tipo_agencia
field :tc_region
field :created_at, :date
field :updated_at, :date
#Custom labels
object_label_method do
:mostrar_nombre_agencia
end
end

So i have on my table tc_tipos_agencias
id: 1, codigo: 'MVX1', nombre: 'some_name'
id: 2, codigo: 'MCC2', nombre: 'another_name'

In dropdown list i get <option value="1">some_name</option> .... and i
need: <option value="MVX1">some_name</option>
because in tc_agencias the foreign_key is tc_tipo_agencia_codigo
instead of tc_tipo_agencia_id.

What can i do to fix this? maybe i'm missing something... do i must to
create a partial for rendering a select with the behaviour i need?

Thanks in advance!

Benoit Bénézech

unread,
Feb 15, 2012, 4:51:16 AM2/15/12
to rails...@googlegroups.com

ejmm320

unread,
Feb 15, 2012, 8:07:05 AM2/15/12
to rails...@googlegroups.com
Thnx for the quick answer, i follow the advice but still doesn't work the way i need, it keeps showing the id instead of codigo.

In dropdown list i get .... <option value="1">some_name</option> .... and i need: ... <option value="MVX1">some_name</option>...

because in tc_agencias the foreign_key is tc_tipo_agencia_codigo instead of  tc_tipo_agencia_id. 

Table tc_tipos_agencias Values
id: 1, codigo: 'MVX1', nombre: 'some_name' 
id: 2, codigo: 'MCC2', nombre: 'another_name' 

The relationship should be "tc_agencia belongs_to tc_tipo_agencia" and the foreign_key of this relation is "tc_tipo_agencia_codigo" (which isn't primary_key, it's a unique_key on database level) not "tc_tipo_agencia_id" even when that field "id" also exists for table tc_tipo_agencia, it seems that RA takes by default the id if exists to create select field from association.

It's not possible for us to use "set_primary_key" because the field codigo in tc_tipos_agencias can be changed by users.

Please help!

Benoit Bénézech

unread,
Feb 16, 2012, 5:39:13 AM2/16/12
to rails...@googlegroups.com
I understand your issue and I understand why it fails in RA.


.map { |o| [o.send(config.object_label_method), o.id] }

This works with the example I gave you because AR use the primary key when no column :id exist.
Thus this was overlooked when implemented.

Instead of o.id, this should be o.send(association_config.foreign_primary_key)


association_config.foreign_primary_key
 
should be set to
 
(association.options[:primary_key] || association.foreign_klass.primary_key)
 
(pseudo code) 

I'm fixing this, but I may need some time because I want to take the chance to spec correctly active_record.rb (to have spec to conform for future other ORMs)

Benoit Bénézech

unread,
Mar 5, 2012, 9:03:27 AM3/5/12
to rails...@googlegroups.com
I just fixed this.
Reply all
Reply to author
Forward
0 new messages