【问题标题】:How to save many has_many_through objects in one form with Rails?如何使用 Rails 以一种形式保存多个 has_many_through 对象?
【发布时间】:2017-12-08 05:43:30
【问题描述】:

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

体育场模型:

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

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

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

  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

好像“team_id”=>“3””作为参数被添加了,但没有插入到任何表格中。而且我现在无法以任何方式输出 Stadiums 和 Teams 之间的关系。

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

任何帮助或指导将不胜感激!

编辑: 体育场参数:

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 => 'StadiumTeam' 添加到球队和体育场模型中,它就奏效了!

【问题讨论】:

  • 你能显示视图代码和你的 Stadium_params 吗?
  • 我还没有完全准备好视图的代码。刚刚尝试通过 rails 控制台访问它。在上面的主帖中添加了 Stadium_params 代码!谢谢!

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


【解决方案1】:

我终于找到了解决办法。

我只是将 :class_name => 'StadiumTeam' 添加到球队和体育场模型中,它就奏效了!

更多信息在这里: has_many :through uninitialized constant

【讨论】:

    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多