Rails 3 has_many through with 3 tables

北城以北 提交于 2019-12-11 08:43:47

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/14761414/rails-3-has-many-through-with-3-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!