0
我想要使用Has並且屬於很多關係來將用戶鏈接到團隊,我遇到的問題是我無法計算出如何將id添加到我的user_teams表中。Ruby on Rails HABTM在表格中添加記錄
在我的模型我已經做了以下
用戶模型
has_and_belongs_to_many :teams
團隊模型
has_many :users
隊控制器
def create
@team = Team.new(params[:team])
respond_to do |format|
if @team.save
@team.users << User.find(current_user)
format.html { redirect_to @team, notice: 'Team was successfully created.' }
format.json { render json: @team, status: :created, location: @team }
else
format.html { render action: "new" }
format.json { render json: @team.errors, status: :unprocessable_entity }
end
end
end
夢幻般的迴應,它已經清除了堆如何設置遷移和關係 –
我很高興,它幫助你。 –