如何编辑has-many-through连接表中的字段

时间:2013-06-30 00:40:34

标签: ruby-on-rails-3.2 inner-join has-many-through

我是一个热心的Rails新手试图弄清楚如何编辑has-many-through连接表中的字段。

我有三个型号。

玩家模型:

# == Schema Information
#
# Table name: players
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Player < ActiveRecord::Base
  attr_accessible :name

  has_many :appearances #, :dependent => true
  has_many :games, :through => :appearances
  accepts_nested_attributes_for :games
  accepts_nested_attributes_for :appearances
end

游戏模型:

# == Schema Information
#
# Table name: games
#
#  id           :integer          not null, primary key
#  team         :string(255)
#  date         :date
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  game_innings :integer
#

class Game < ActiveRecord::Base
  attr_accessible :date, :team, :game_innings

  has_many :appearances   #, :dependent => true
  has_many :players, :through => :appearances
  accepts_nested_attributes_for :players
  accepts_nested_attributes_for :appearances

end

外观模型:

# == Schema Information
#
# Table name: appearances
#
#  id             :integer          not null, primary key
#  player_id      :integer          not null
#  game_id        :integer          not null
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#  innings_played :integer
#

class Appearance < ActiveRecord::Base
  attr_accessible :player_id, :game_id

  belongs_to :game
  belongs_to :player
end

当我编辑一个玩家时,我希望能够列出并编辑每个游戏中玩的局,或者更具体地说,我想列出并编辑每个外观的innings_played。

我意识到它不完整,但这是我的edit.html.erb目前的样子:

<h1>Editing player</h1>

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

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

    <div class="field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
    </div>

    <table>
        <tr>
            <th>Opponent</th>
            <th>Innings Played</th>
        </tr>

        <% f.fields_for :games do |games_form| %>
            <tr>
                <td><%= games_form.label :team %></td>
            </tr>
        <% end %>


    </table>

    <br>
    <br>

    <div class="actions">
        <%= f.submit %>
    </div>
<% end %>

<%= link_to 'Show', @player %> |
<%= link_to 'Back', players_path %>

感谢您可以分享的任何知识。

1 个答案:

答案 0 :(得分:0)

如下所示更改播放器和外观,请注意每个班级顶部的:appearances_attributes:game_attributes

class Player < ActiveRecord::Base
  attr_accessible :name, :appearances_attributes

  has_many :appearances #, :dependent => true
  has_many :games, :through => :appearances

  accepts_nested_attributes_for :appearances
end

class Appearance < ActiveRecord::Base
  attr_accessible :player_id, :game_id, :innings_played, :game_attributes

  belongs_to :game
  belongs_to :player

  accepts_nested_attributes_for :game
end

将视图更改为以下内容,因为我不确切知道您要在视图中显示的内容,所以我只举一个示例,您可能需要更改它以满足您的情况。

<table>
   <tr>            
   <th>Innings Played</th>
   <th>Team</th>
   </tr>
     <%= f.fields_for :appearances do |appearances_form|%>                              

       <%= appearances_form.fields_for :game do |game_form| %>
         <tr>                               
            <td><%= appearances_form.text_field :innings_played %></td>
            <td><%= game_form.text_field :team %></td>
         </tr>
       <% end %>

    <% end %>

</table>