2

My model "combobox" has_many "comboboxselects", and "comboboxselects" belongs_to "combobox". Activescaffold of "comboboxes" displays data in comboboxselects-column like "#<Comboboxselect:0x472d25c>". How to make display the "answer" column from table "comboxselects"?

Models:

class Combobox < ActiveRecord::Base
 has_many :comboboxselects
end

class Comboboxselect < ActiveRecord::Base
 belongs_to :combobox
end

Schema:

  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

Output:

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
kubum
  • 469
  • 2
  • 13

2 Answers2

1

First, all fields referenced in config.list.columns have to be included in config.columns (any explicitly-defined config.*.columns fields must be subsets of config.columns).

Then, in each model that does not already have a name or title field or method, you have to declare this custom method:

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

See ActiveScaffold documentation: Describing Records: to_label

Ryan Barton
  • 313
  • 3
  • 12
0

When you say displays I assume you mean in a view? Can you post the code your running to get that output.

Looks to me like you just have Comboboxselect object, have you tried adding .answer to it to access the attribute you want?

tsdbrown
  • 5,038
  • 3
  • 36
  • 40
  • Yes, in an activescaffold default view. I have added the activescaffold default loading config. Hm.. You are right, and I don't now how to output the :answer. – kubum May 18 '09 at 20:31