首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Rails以一种形式保存多个has_many_through对象?

如何使用Rails以一种形式保存多个has_many_through对象?
EN

Stack Overflow用户
提问于 2017-12-04 14:04:19
回答 1查看 50关注 0票数 0

我正在开发一个应用程序,其中我有两个表,它们之间有着多到多的关系,中间有一个连接表。

体育场模型:

代码语言:javascript
复制
class Stadium < ActiveRecord::Base

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

团队模型:

代码语言:javascript
复制
class Team < ActiveRecord::Base
    has_many :stadiumteams
    has_many :stadiums, :through => :stadiumteams
end

StadiumTeam模型(连接表):

代码语言:javascript
复制
class StadiumTeam < ActiveRecord::Base
belongs_to :stadium
belongs_to :team
end

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

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

代码语言:javascript
复制
  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

当我试图创建一个新的体育场时,我从航站楼得到了这个输出。

代码语言:javascript
复制
  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”是作为一个参数添加的,但是它没有插入到任何表中,而且我现在不能以任何方式输出体育场和球队之间的关系。

我想要完成的是能够在球场展示页面上展示所有与球场相关的球队。以及“团队秀”页面中与团队相关的所有体育场馆。

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

编辑:体育场广场:

代码语言:javascript
复制
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: SOLVED:

在和这个做斗争之后。我终于找到了一个解决方案:many :through uninitialized constant

我只是简单地加入了:class_name => 'StadiumTeam‘到球队和体育场的模型中,它起了作用!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-07 21:43:30

我终于找到了解决这个问题的办法。

我只是简单地加入了:class_name => 'StadiumTeam‘到球队和体育场的模型中,它起了作用!

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47635152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档