gpt4 book ai didi

mysql - "Can' t 使用自定义外键时将未知属性写入HABTM连接表

转载 作者:行者123 更新时间:2023-11-28 23:34:33 24 4
gpt4 key购买 nike

用户可以是 HABTM 游戏,而游戏可以是 HABTM 用户作为讲故事的人,以区别于以后的关系,后者的游戏将有许多用户作为参与者。

当我尝试通过 Games Controller 中的 create 方法将用户输入的 User 对象添加到 @game.storytellers 时,Rails 抛出错误“无法写入未知属性 ‘user_id’”。

我很确定问题出在我上次迁移(在本文底部)。

模型/用户.rb

class User < ActiveRecord::Base
has_and_belongs_to_many :games, association_foreign_key: "storyteller_id"
end

模型/游戏.rb

class Game < ActiveRecord::Base
has_and_belongs_to_many :storytellers, class_name: "User",
foreign_key: "storyteller_id"
attr_accessor :storyteller_group
end

Controller /游戏.rb

  def create
@game = Game.new(game_params)
@game.storytellers << params[:game][:storyteller_group].split(",").collect { |n| User.find_by(name: n) }

respond_to do |format|
if @game.save
format.html { redirect_to @game, notice: 'Game was successfully created.' }
format.json { render :show, status: :created, location: @game }
else
format.html { render :new }
format.json { render json: @game.errors, status: :unprocessable_entity }
end
end
end

views/games/_form.html.erb

<%= form_for(@game) do |f| %>
[snip]

<!-- Additional storytellers-->
<div class="field">
<%= f.label :storyteller_group, id: "create-game" %>
<div class="input">
<%= f.text_field :storyteller_group, value: current_user.name %>
</div>
</div>

[snip]
<% end %>

db/migrations/create_users.rb

class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.index :name

t.string :email
t.index :email

[snip]

t.timestamps
end
end
end

db/migrations/create_games.rb

Class CreateGames < ActiveRecord::Migration
def change
create_table :games do |t|
t.string :name
t.text :description
t.string :source

t.timestamps
end
end
end

db/migrations/games_users.rb

class GamesUsers < ActiveRecord::Migration
def change
create_table :games_users, id: false do |t|
t.belongs_to :game, index: true
t.integer :storyteller_id, index: true
end
end
end

最佳答案

您以相反的方式编写了 HABTM 外键定义。引自 Rails guides :

:association_foreign_key

By convention, Rails assumes that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix _id added. The :association_foreign_key option lets you set the name of the foreign key directly.

:foreign_key

By convention, Rails assumes that the column in the join table used to hold the foreign key pointing to this model is the name of this model with the suffix _id added. The :foreign_key option lets you set the name of the foreign key directly

因此,以下应该有效:

class User < ActiveRecord::Base
has_and_belongs_to_many :games, foreign_key: "storyteller_id"
end

class Game < ActiveRecord::Base
has_and_belongs_to_many :storytellers, class_name: "User",
association_foreign_key: "storyteller_id"
end

关于mysql - "Can' t 使用自定义外键时将未知属性写入HABTM连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com