ejmm320
unread,Feb 14, 2012, 2:40:30 PM2/14/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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!