1

用这个敲打我的头,我问过类似的问题,但一无所获。所以我有三张桌子 Superheros、Powers 和 Teams。一个超级英雄可以有许多权力和许多团队,一个权力可以与许多超级英雄相关,而一个团队由许多超级英雄组成。我决定将这些保存到一个大的关系表(我称之为 Marvel)

这是我设置的:

class Superhero < ActiveRecord::Base
  attr_accessible :name, :power_ids, :team_ids, :attributes_marvels

  has_many :marvels, :dependent => :destroy
  has_many :powers, :through => :marvels
  has_many :teams, :through => :marvels
  accepts_nested_attributes_for :marvels
end

class Power < ActiveRecord::Base
  attr_accessible :name

  has_many :marvels, :dependent => :destroy
end

class Team < ActiveRecord::Base
  attr_accessible :name

  has_many :marvels, :dependent => :destroy
end

class Marvel < ActiveRecord::Base
  attr_accessible :power_id, :superhero_id, :team_id

  belongs_to :superhero
  belongs_to :power
  belongs_to :team
end

这是创建超级英雄的表格:

<%= form_for(@superhero) do |f| %>
 <div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name %>
 </div>
<div class="field">
 <p>Please select which team you work for:</p>
 <%= f.collection_select(:team_ids, Team.all(:order=>:name), :id, :name, {:prompt => true}) %>
 </div>
 <div class="field">
<p>Please check all powers that apply to you.</p>
<% Power.all.each do |power| %>
     <label class="checkbox">
     <%= check_box_tag "superhero[power_ids][]", power.id, @superhero.power_ids.include?(power.id) %>
     <%= power.name %>
    </label>
    <% end %>
 </div>
 <div class="actions">
  <%= f.submit %>
 </div>
<% end %>

超级英雄控制器

  def new
   @superhero = Superhero.new
  end

  def create
   @superhero = Superhero.new(params[:superhero])
  end

我创建这种方式是因为我希望能够在表单中询问(复选框)权力列表,(下拉)团队列表,现在根据这些标准向我显示超级英雄列表。

我几乎可以正常工作,但是保存时遇到问题,我在记录器文件中注意到它给了我这个:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"sgD0NIjDM8QhMdzSsb7M/+PFd+FxMtNeOGukrdw9qFA=", "superhero"=>{"name"=>"Storm", "team_ids"=>"3", "power_ids"=>["1"]}, "commit"=>"Create Superhero"}

INSERT INTO "superheros" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", Thu, 07 Feb 2013 19:59:24 UTC +00:00], ["name", "Storm"], ["updated_at", Thu, 07 Feb 2013 19:59:24 UTC +00:00]]

INSERT INTO "marvels" ("created_at", "power_id", "superhero_id", "team_id", "updated_at") VALUES (?, ?, ?, ?, ?)[0m  [["created_at", Thu, 07 Feb 2013 19:59:24 UTC +00:00], ["power_id", nil], ["superhero_id", 8], ["team_id", 3], ["updated_at", Thu, 07 Feb 2013 19:59:24 UTC +00:00]]

INSERT INTO "marvels" ("created_at", "power_id", "superhero_id", "team_id", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Thu, 07 Feb 2013 19:59:24 UTC +00:00], ["power_id", 1], ["superhero_id", 8], ["team_id", nil], ["updated_at", Thu, 07 Feb 2013 19:59:24 UTC +00:00]]

为什么它会插入两次,当您可以清楚地看到参数中的值时,为什么会有 Nil 值?

4

1 回答 1

1

您需要在表的两侧创建依赖关系:

class Superhero < ActiveRecord::Base
  attr_accessible :name, :power_ids, :team_ids, :attributes_marvels

  has_many :marvels, :dependent => :destroy
  has_many :powers, :through => :marvels
  has_many :teams, :through => :marvels
  accepts_nested_attributes_for :marvels
end

class Power < ActiveRecord::Base
  attr_accessible :name

  `has_many :superheros, :through => :marvels`
  has_many :marvels, :dependent => :destroy
end

class Team < ActiveRecord::Base
  attr_accessible :name

  `has_many :superheros, :through => :marvels`
  has_many :marvels, :dependent => :destroy
end

class Marvel < ActiveRecord::Base
  attr_accessible :power_id, :superhero_id, :team_id

  belongs_to :superhero
  belongs_to :power
  belongs_to :team
end

通过这种方式,您可以在 Marvels 中为每个模型添加 ID 密钥,并且可以访问它们,请在 assosiations guide 中查看

于 2013-04-05T20:35:50.697 回答