如何使用Rails在一个表单中保存许多has_many_through对象?

时间:2017-12-04 14:04:20

标签: ruby-on-rails many-to-many

我正在开发一个应用程序,其中我有两个具有多对多关系的表,在中间通过连接表连接。

体育场模型:

class Stadium < ActiveRecord::Base

has_many :stadiumteams
has_many :teams, :through => :stadiumteams

团队模型:

class Team < ActiveRecord::Base
    has_many :stadiumteams
    has_many :stadiums, :through => :stadiumteams
end

StadiumTeam模型(加入表):

class StadiumTeam < ActiveRecord::Base
belongs_to :stadium
belongs_to :team
end

当我创建一个新的体育场时,我希望能够选择一个或多个与体育场相关联的球队,并将关系保存到联赛表(sportsteams)。

以下是体育场控制器的创建操作。

  def create
    @stadium = current_user.stadiums.build(stadium_params)

    respond_to do |format|
      if @stadium.save
         teams = Team.where(:id => params[:team])
         teams.each {|team| @stadium.teams << team}

        format.html { redirect_to @stadium, notice: 'Stadium was successfully created.' }
        format.json { render :show, status: :created, location: @stadium }
      else
        format.html { render :new }
        format.json { render json: @stadium.errors, status: :unprocessable_entity }
      end
    end
  end

当我尝试创建一个新的体育场时,我从终端获得此输出。

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"m+HAw4LRPmozIkigzuB/SFdcAFPTL735n6FyavH0SwjGXl7NHCLmDCXMTvx8bVixz+sL7tfn1zzCl04Q+w6Pdw==", "stadium"=>{"name"=>"Friends Arena", "capacity"=>"100000", "surface"=>"", "official_opening_date(1i)"=>"2017", "official_opening_date(2i)"=>"12", "official_opening_date(3i)"=>"4", "cost"=>"10000", "web_url"=>"teststadium.com", "record_attendance"=>"999790", "city"=>"", "country"=>"", "location_name"=>"", "longitude"=>"", "latitude"=>"", "address"=>"Friends Arena, Solna, Sweden"}, "team_id"=>"3", "commit"=>"Create Stadium"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.2ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "stadiums" ("name", "capacity", "city", "country", "location_name", "address", "surface", "cost", "web_url", "record_attendance", "official_opening_date", "user_id", "latitude", "longitude", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["name", "Friends Arena"], ["capacity", 100000], ["city", "Solna"], ["country", "Sweden"], ["location_name", ""], ["address", "Friends Arena, Solna, Sweden"], ["surface", ""], ["cost", 10000.0], ["web_url", "teststadium.com"], ["record_attendance", 999790], ["official_opening_date", "2017-12-04"], ["user_id", 1], ["latitude", 59.3727005], ["longitude", 18.0002317], ["created_at", "2017-12-04 13:18:45.841931"], ["updated_at", "2017-12-04 13:18:45.841931"]]
   (2.8ms)  commit transaction

好像&#34; team_id&#34; =&gt;&#34; 3&#34;&#34;是作为参数添加的,但它没有插入任何表中。而且我现在无法以任何方式输出体育场与球队之间的关系。

我想要完成的是能够在球场展示页面上显示与球场相关的所有球队。所有与球队团队相关联的体育场显示页面。

非常感谢任何帮助或指导!

修改 体育场参数:

def stadium_params
  params.require(:stadium).permit(:name, :capacity, :city, :country, :location_name, :address, :longitude, :latitude, :image, :surface, :official_opening_date, :cost, :web_url, :also_known_as, :record_attendance)
end

EDIT2:已解决:

在与此挣扎之后。我终于找到了一个解决方案:has_many :through uninitialized constant

我只是添加了:class_name =&gt; &#39; StadiumTeam&#39;对团队和体育场模型而言,它有效!

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。

我只是添加了:class_name =&gt; &#39; StadiumTeam&#39;对团队和体育场模型而言,它有效!

更多信息: has_many :through uninitialized constant