1

I created two models:

Answers AnswersTypes {id, text}

The latter one could accept two values {correct / wrong} as text.

I modified the admin panel to accept an html select field to link an AnswerType directly within the Answer.

Now the issue is that in the Answer's list page I have a column AnswerType in which i could see the ID of the linked AnswerType but I would like to see the text instead of the ID.

In the file /admin/answers/views/index.erb the field is:

<td><%= answer.answer_type %=></td>

How could I modify it to select the text starting from that id !?

Thank you very much and ... I have to say that Padrino Rocks!

EDIT: adding some infos SCHEMA create_table "answer_types" t.string "text" [..]

create_table "answers"
    t.string "text"
    t.integer "answer_type_id"
[..]

models/answer.rb class Answer < ActiveRecord::Base [..] has_one :answerType

models/answer_types.rb

class AnswerTypes < ActiveRecord::Base [..] belongs_to :answer

2 Answers2

0

You should be able to retrieve the text value from an AnswerType object by calling text on the object:

<td><%= answer.answer_type.text %></td>
Charles Worthington
  • 1,110
  • 8
  • 11
  • Thanks Charles, I tried it but it doesn't work. In my table I have: answer_types (ID | text) 1 | right 2 | wrong answer (ID | text | answer_id) 1 | foo | 1 Right now, when i click on the Answer's List Padrino retrieves: foo | 1 Instead of that result I would like to receive "foo | right". If I apply your code, Padrino says: NoMethodError at /admin/answers undefined method 'text' for 2:Fixnum Thanks – Claudio Bellucci Mar 06 '13 at 17:08
  • Hmm, does it give you an error? What do you see if you put `<%= answer.answer_type.inspect %>`? – Charles Worthington Mar 06 '13 at 17:11
  • Sorry Charles, I modified my own answer :) With the inspect, I receive the correct ID. Like If I use just answer.answer_type. – Claudio Bellucci Mar 06 '13 at 17:13
  • It seems like the association may not be set up appropriately. One thing I noticed is that in your `Answer` model, your column should be named `answer_type_id`. Did you set up `:has_one` and `:belongs_to` associations in your `Answer` and `AnswerType` models? – Charles Worthington Mar 06 '13 at 17:25
  • Hi Charles, I added some infos in a separate answer! – Claudio Bellucci Mar 07 '13 at 14:13
0

I try to give you some more informations:

In db/schema.rb I have:

create_table "answer_types", :force => true do |t|
  t.string "text"
end

create_table "answers", :force => true do |t|
  t.string "text"
  t.integer "answer_type_id"
end

In models/answer.rb I have:

class Answer < ActiveRecord::Base
    validates_presence_of :text
    belongs_to :question
    has_one :answer_type
end

In *models/answer_types.rb* I have: class AnswerTypes < ActiveRecord::Base belongs_to :answer end

So in admin/views/answers/index.erb I have:

<td><%= answer.answer_type_id %></td>

Which prints correctly the answer_type_id linked to the answer.

Coming back on the initial question: how could I replace the answer_type_id printing the answer_types text field?

Thanks!

  • I found out the problem! It was really silly!! Simply within the **answer** model I named the *belongs_to* association *:answerType* instead of **answer_type** !! Right now I could access the answer_type title just typing **answer.answer_type.title**! Thanks to Charles for the help! – Claudio Bellucci Mar 07 '13 at 16:39
  • Glad you got it figured out! – Charles Worthington Mar 07 '13 at 16:42