Rails 3拥有3个表

时间:2013-02-07 21:34:43

标签: ruby-on-rails-3 database-design has-many-through belongs-to

用这个敲我的脑袋,我问了类似的东西,但却无处可去。所以我有三个表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

我创造了这种方式,因为我希望能够以一种形式询问,(复选框)权力列表,(下拉列表)团队列表,现在显示基于这些标准的超级英雄列表。 / p>

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

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值?

1 个答案:

答案 0 :(得分: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键,并且可以全部访问它们,在协会指南中查看