ActiveScaffold - 更改关联对象的默认名称

时间:2009-05-17 10:33:15

标签: ruby-on-rails ruby activescaffold

我的模型"combobox" has_many "comboboxselects""comboboxselects" belongs_to "combobox"。 “comboboxes”的Activescaffold在comboboxselects列中显示数据,如"#<Comboboxselect:0x472d25c>"。如何从“comboxselects”表中显示“答案”栏?

型号:

class Combobox < ActiveRecord::Base
 has_many :comboboxselects
end

class Comboboxselect < ActiveRecord::Base
 belongs_to :combobox
end

架构:

  create_table "comboboxes", :force => true do |t|
   t.string   "question"
   t.datetime "created_at"
   t.datetime "updated_at"
  end

  create_table "comboboxselects", :force => true do |t|
   t.integer  "combobox_id"
   t.string   "answer"
   t.datetime "created_at"
   t.datetime "updated_at"
  end

输出:

class ComboboxesController < ApplicationController
 active_scaffold :combobox do |config|
   config.list.columns = [:id, :question]
   config.columns = [:question, :comboboxselects]
 end
end

class ComboboxselectsController < ApplicationController
 active_scaffold :comboboxselect  do |config|
   config.list.columns = [:id, :combobox, :answer]
   config.columns = [:answer]
 end
end

2 个答案:

答案 0 :(得分:1)

首先,config.list.columns中引用的所有字段都必须包含在config.columns中(任何显式定义的config。*。columns字段必须是config.columns的子集)。

然后,在每个尚未具有名称或标题字段或方法的模型中,您必须声明此自定义方法:

class Comboboxselect < ActiveRecord::Base
 belongs_to :combobox
 def to_label
  "#{answer}" 
 end
end

请参阅ActiveScaffold文档:Describing Records: to_label

答案 1 :(得分:0)

当你说显示我认为你在视图中的意思?你可以发布你运行的代码来获得输出。

在我看来,您只需拥有Comboboxselect对象,是否尝试向其中添加.answer以访问您想要的属性?