我正在开发一个应用程序,其中我有两个表,它们之间有着多到多的关系,中间有一个连接表。
体育场模型:
class Stadium < ActiveRecord::Base
has_many :stadiumteams
has_many :teams, :through => :stadiumteams团队模型:
class Team < ActiveRecord::Base
has_many :stadiumteams
has_many :stadiums, :through => :stadiumteams
endStadiumTeam模型(连接表):
class StadiumTeam < ActiveRecord::Base
belongs_to :stadium
belongs_to :team
end当我创建一个新的体育场时,我希望能够选择一个或多个与体育场相关的球队,并保存与联合表(体育场球队)的关系。
下面是创建体育场控制器的操作。
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”是作为一个参数添加的,但是它没有插入到任何表中,而且我现在不能以任何方式输出体育场和球队之间的关系。
我想要完成的是能够在球场展示页面上展示所有与球场相关的球队。以及“团队秀”页面中与团队相关的所有体育场馆。
任何帮助或指导将是非常感谢的!
编辑:体育场广场:
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)
endEDIT2: SOLVED:
在和这个做斗争之后。我终于找到了一个解决方案:many :through uninitialized constant
我只是简单地加入了:class_name => 'StadiumTeam‘到球队和体育场的模型中,它起了作用!
发布于 2017-12-07 21:43:30
我终于找到了解决这个问题的办法。
我只是简单地加入了:class_name => 'StadiumTeam‘到球队和体育场的模型中,它起了作用!
https://stackoverflow.com/questions/47635152
复制相似问题