user1339378
user1339378

Reputation: 35

Rails saving 0 instead of string from model collection_select

I have been trying to create a basic rails application. I used generate to create a basic scaffold and I have setup a belongs_to relationship between players and teams so players belong_to team and team has_many players.

My form view looks like this

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

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

  <div class="field">
    <%= f.label :Playerone %><br />
    <%= f.collection_select :Playerone, Player.all, :firstname, :firstname %>
  </div>

  <div class="field">
    <%= f.label :Playertwo %><br />
    <%= f.collection_select :Playertwo, Player.all, :firstname, :firstname %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

When I try to create a new team I get the dropdowns as I want them with the players first name to select from but then when it is saved it saves 0 as the record.

Listing teams

Playerone Playertwo
0 0 Show Edit Destroy

New Team

Originally I had the collection select like this <%= f.collection_select :Playertwo, Player.all, :id, :firstname %> This inserts the ID but I want the text to be inserted instead.

I have looked over reams and reams of documentation and have come up trumps as I am still learning.

Thanks for any help :)

Upvotes: 1

Views: 154

Answers (1)

Marlin Pierce
Marlin Pierce

Reputation: 10081

Then it depends on your migration. If you created an integer field in your database, saving the string will save 0.

You can (a), change your migration to use a string instead of an integer.

You can (b), use the id, and display the name by looking up the name.

f.collection_select :Playertwo, Player.all, :id, :firstname

You can either get the name from the team belongs_to playerone and playertwo,

class Team
  belongs_to :playerone, :class_name => "Player"
  belongs_to :playertwo, :class_name => "Player"
end

<%= team.playerone.firstname %>

or you can delegate the firstname to the players.

class Team
  belongs_to :playerone, :class_name => "Player"
  belongs_to :playertwo, :class_name => "Player"
  delegate :firstname, :to => :playerone, :prefix => true, :allow_nil => true
  delegate :firstname, :to => :playertwo, :prefix => true, :allow_nil => true
end

<%= team.playerone_firstname %>

Or you can (c) use the belongs_to, which uses an integer. Please look up the documentation http://guides.rubyonrails.org/getting_started.html

Upvotes: 3

Related Questions