0
votes

I want to store multiple questions and answers with a unique player ID in one database column.

Database column player_answer should look like-

player_id | question | answer

1 | India | New Delhi

1 | USA | Washington D.C.

1 | UK | London

..so on and so forth.

This is my /views/player_answers/_form.html.erb

<%= form_for(@player_answer) do |f| %>
  <% if @player_answer.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@player_answer.errors.count, "error") %> prohibited this player_answer from being saved:</h2>

      <ul>
      <% @player_answer.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :player_id %><br />
    <%= f.text_field :player_id, :readonly => true, :value => Player.find(params[:playerid]).id %>
    <!--<#%= f.text_field :player_id %>-->
  </div>

<% (1..10).each do |i| puts i %>

<table>
  <tr>

    <td>
  <div class="field">

    <%= f.label :question_id %><br />
    <%= f.text_field :question_id %>

  </div>
    </td>
    <td>
  <div class="field">
    <%= f.label :answer %><br />
    <%= f.text_field :answer, :readonly => true, :value => params[:playeranswerid].to_i %>
  </div>
      </td>

  </tr>
</table>


<% end  %>


  <div class="field">
    <%= f.label :answer_after_self_scoring %><br />
    <%= f.text_field :answer_after_self_scoring %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And here is my player_answers_controller.rb file code- class PlayerAnswersController < ApplicationController # GET /player_answers # GET /player_answers.xml def index @player_answers = PlayerAnswer.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @player_answers }
end

end

# GET /player_answers/1 # GET /player_answers/1.xml def show @player_answer = PlayerAnswer.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @player_answer }
end

end

# GET /player_answers/new # GET /player_answers/new.xml def new @player_answer = PlayerAnswer.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @player_answer }
end

end

# GET /player_answers/1/edit def edit @player_answer = PlayerAnswer.find(params[:id]) end

# POST /player_answers # POST /player_answers.xml def create @player_answer = PlayerAnswer.new(params[:player_answer])

respond_to do |format|
  if @player_answer.save
    format.html { redirect_to(@player_answer, :notice => 'Player answer was successfully created.') }
    format.xml  { render :xml => @player_answer, :status => :created, :location => @player_answer }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @player_answer.errors, :status => :unprocessable_entity }
  end
end

end

# PUT /player_answers/1 # PUT /player_answers/1.xml def update @player_answer = PlayerAnswer.find(params[:id])

respond_to do |format|
  if @player_answer.update_attributes(params[:player_answer])
    format.html { redirect_to(@player_answer, :notice => 'Player answer was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @player_answer.errors, :status => :unprocessable_entity }
  end
end

end

# DELETE /player_answers/1 # DELETE /player_answers/1.xml def destroy @player_answer = PlayerAnswer.find(params[:id]) @player_answer.destroy

respond_to do |format|
  format.html { redirect_to(player_answers_url) }
  format.xml  { head :ok }
end

end end

Please let me know how to go ahead and solve this problem.

P.s. I have just started using rails and this is probably the first project I am working live on. Please help!!

2

2 Answers

1
votes

If you want to add multiple data in one column, use 'serialize'.

This would store everything as a Hash or an Array, as you prefer.

See doc here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html

0
votes

it is not clear from your question what are you having troubles with

do you get error when you are trying to create new records with same player_id? if so - make sure player_id does not have uniq index or is not primary key

to create table/model for your layout try this:

script/generate migration create_player_answers player_id:integer question:string answer:string

or rais generate - as you didnt specify which rails version you're using