Rails 3 has_many通过3个表 [英] Rails 3 has_many through with 3 tables
问题描述
这是我设置的:
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
acceptance_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>请选择您的工作组:< / p>
<%= f.collection_select(:team_ids,Team.all(:order =>:name),:id,:name,{:prompt => true})%>
< / div>
< div class =field>
< p>请检查适用于您的所有权力。< / p>
<%Power.all.each do | power | %>
< label class =checkbox>
<%= check_box_tagsuperhero [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
我已经创建了这样的方式,因为我想要能够在一个表单请问,(复选框)权力列表(下拉列表)一个团队列表,现在显示了一个基于这些条件的超级英雄列表。
我有这个几乎工作但存在问题,我注意到在记录器文件它给我这样的:
参数:{utf8 = power_ids=> [1]},commit=>创建超级英雄}
INSERT INTOsuperheros(created_at,name,updated_at VALUES(?,?,?)[[created_at,Thu,2013年2月7日19:59:24 UTC +00:00],[name,Storm],[updated_at,Thu,07 Feb 201319:59:24 UTC +00:00]]
INSERT INTOmarvels(created_at,power_id,superhero_id,team_id,updated_at)VALUES ,[...],[...] [0 [[created_at,Thu,07 Feb 2013 19:59:24 UTC +00:00],[power_id,nil],[superhero_id,8] [team_id,3],[updated_at,Thu,2013年2月7日19:59:24 UTC +00:00]]
INSERT INTOmarvels(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
acceptance_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指导
Banging my head with this one, I've asked something similar, but getting nowhere with it. So I have three tables Superheros, Powers, and Teams. A superhero can have many powers and many teams, a power can relate to many superheros, and a team consists of many superheros. I've decided to save these to a big relationship table (which I call Marvel)
Here's what I have set up:
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
Here's the form to create the superhero:
<%= 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 %>
Controller for superhero
def new
@superhero = Superhero.new
end
def create
@superhero = Superhero.new(params[:superhero])
end
I've created this way because I want to be able to in a form ask, (checkboxes)A list of powers, (dropdown)A list of teams, now show me a list of superheros based on those criteria.
I've got this almost working but having problems with the save, I noticed in the logger file it's giving me this:
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]]
why is it inserting twice and why are there Nil values when you can plainly see the values in the parameters?
You need to create dependancies in both sides of the tables:
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
this way you add ID keys in Marvels for each model and ull be able to acces them all, check it at assosiations guide
这篇关于Rails 3 has_many通过3个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!