我用这个来敲我的头,我问了一些类似的问题,但没有成功。所以我有三张表超级英雄、力量和团队。一个超级英雄可以拥有许多力量和团队,一种力量可以与许多超级英雄联系在一起,一个团队由许多超级英雄组成。我决定把这些保存到一个大的关系表(我称之为漫威)中
以下是我设置的:
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]]
为什么它要插入两次,当你可以清楚地看到参数中的值时,为什么会有零值?
您需要在表的两侧创建依赖关系:
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密钥,并且可以访问所有密钥,请在关联指南