I'm working on an app where I have two Tables that have a many-to-many relationship, joined in the middle by a join table.
Stadium model:
class Stadium < ActiveRecord::Base
has_many :stadiumteams
has_many :teams, :through => :stadiumteams
Team model:
class Team < ActiveRecord::Base
has_many :stadiumteams
has_many :stadiums, :through => :stadiumteams
end
StadiumTeam model (the jointable):
class StadiumTeam < ActiveRecord::Base
belongs_to :stadium
belongs_to :team
end
When I create a new Stadium I want to be able to select one or multiple teams that are associated with a stadium and save the relationship to the join-table (stadiumteams).
Here's the create action form the Stadiums Controller.
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
When I try to create a new Stadium I get this output from the terminal.
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
It seems like "team_id"=>"3"" is added as a parameter but it's not inserted into any table. And I can't output the relationship between Stadiums and Teams in any way right now.
What I want to accomplish is to be able to show all Teams that are associated with Stadium on the Stadium show page. And all Stadiums that are associated with a Team on the teams Show page.
Any help or guidance would be very appreciated!
EDIT: Stadium params:
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:
After struggling with this. I finally found a solution: has_many :through uninitialized constant
I simply added :class_name => 'StadiumTeam' to the team and stadium model and it worked!