I have a logic query that I'm not sure how to solve, I'm doing a kind of survey, in which, when creating the questions, I need to specify the type of response that it will receive, either a text string , a boolean data, a numeric data, etc. However, I have the database field saved by default as "string", within the "Answer" table, how could I allow any type of data, specifically in the field called "response" and that at the time from calling it on view return as such? I appreciate your suggestions
question.rb
class CreateQuestions < ActiveRecord::Migration[5.2]
def change
create_table :questions do |t|
t.string :title
t.references :survey, foreign_key: true
t.timestamps
end
end
end
answer.rb
class CreateAnswers < ActiveRecord::Migration[5.2]
def change
create_table :answers do |t|
t.string :response
t.references :question, foreign_key: true
t.timestamps
end
end
end
What I would do is serialize the field.
And in the db leave the field as
t.text :response
instead ofstring
, so you would save any type of data in the same field, including hashes or objects, and then you would recover it as it is when consulting them.