Rails 3 has_many通过3表

问题描述:

我的头,这个问题,我问过类似的东西,但没有得到它。所以我有三张表Superheros,Powers和Teams。一个超级英雄可以拥有许多权力和很多团队,权力可以与许多超级英雄联系在一起,而一个团队由许多超级英雄组成。我决定将这些保存到一个大的关系表(我称之为奇迹)Rails 3 has_many通过3表

以下是我已经设置了:

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 

,因为我希望能够在表单中问我创建了这种方式,(CHEC kboxes)权力列表(下拉列表)团队列表现在向我展示基于这些标准的超级英雄列表。

我有这种近乎工作,但具有与该保存的问题,我在记录注意到文件的时候给我这个:

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]] 

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

您需要在表格中双方建立依赖关系:

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 

这样你在奇迹添加ID键为每个模型和ULL能够存取权限所有这些,在assosiations指导检查

+0

我有相同类型的问题,但在这里更复杂一点:http://*.com/questions/26028291/data-modeling-3-way-table-has-many-association任何adice? – Sauron