Rails:创建不通过关联保存has_many的操作-“回滚事务”

考拉里克

放心,我才刚刚开始学习Rails,这是我在这里的第一个问题!

我正在学习的项目是排球记分牌,所以现在我正在尝试构建一个表单来提交2v2游戏的得分。我有用户游戏,它们由has_many通过与参与者联接表的关系来关联,参与者联接表还包括“结果”属性(“ W”或“ L”)。

我的问题是,当我提交时失败,并且没有创建参与者如果我从表单中删除了关联,则提交仅适用于游戏参数。

希望我在下面提供了所有相关信息。另外,如果有更好的方法来完成所有这些操作,我很想听听!

楷模

class Game < ApplicationRecord
  has_one :venue
  has_many :participants
  has_many :users, through: :participants

  accepts_nested_attributes_for :participants,
    reject_if: :all_blank, allow_destroy: true
end

class User < ApplicationRecord
  has_many :participants
  has_many :games, through: :participants
end

class Participant < ApplicationRecord
  belongs_to :game
  belongs_to :user
end

施玛

create_table "games", force: :cascade do |t|
    t.date     "game_date"
    t.integer  "winning_score"
    t.integer  "losing_score"
    t.text     "notes"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.integer  "venue_id"
    t.index ["venue_id"], name: "index_games_on_venue_id"
end

create_table "participants", force: :cascade do |t|
  t.integer  "user_id"
  t.integer  "game_id"
  t.string   "result"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["game_id"], name: "index_participants_on_game_id"
  t.index ["user_id"], name: "index_participants_on_user_id"
end

create_table "users", force: :cascade do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at",      null: false
  t.datetime "updated_at",      null: false
  t.string   "password_digest"
  t.string   "remember_digest"
  t.index ["email"], name: "index_users_on_email", unique: true
end

create_table "venues", force: :cascade do |t|
  t.string   "name"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

控制器

class GamesController < ApplicationController

  def show
    @game = Game.find(params[:id])
  end

  def new
    @users = User.all
    @game = Game.new
    @game.participants.build
  end

  def create
    @game = Game.new(game_params)

    if @game.save
      redirect_to 'show'
    else
      render 'new'
    end
  end

  private

    def game_params
      params.require(:game).permit(:game_date, :winning_score,
                                   :losing_score, :notes, :venue_id,
                                   participants_attributes: [:user_id, :result, 
                                   :_destroy])
    end

end

形式

<%= simple_form_for @game do |f| %>
  <div id="winners">
    <b>Winners</b>
    <% for i in 0..1 %>
      <%= f.simple_fields_for :participants do |p| %>
        <%= p.association :user, :collection => @users, label: false %>
        <%= p.input :result, :as => :hidden, :input_html => { :value => 'W' }%>
      <% end %>
    <% end %>
  </div>

  <%= f.input :winning_score, :collection => 15..30 %>

  <div id="losers">
    <b>Losers</b>
    <% for i in 2..3 %>
      <%= f.simple_fields_for :participants do |p| %>
        <%= p.association :user, :collection => @users, label: false %>
        <%= p.input :result, :as => :hidden, :input_html => { :value => 'L' }%>
      <% end %>
    <% end %>
  </div>

  <%= f.input :losing_score, :collection => 0..30 %>

  <%= f.input :notes %>

  <%= f.submit "Submit!", class: "btn btn-primary" %>

<% end %>

回复

Processing by GamesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"p8081+wU7EqYV7PIIAOGP3N+Md4CJusFpL9qTm3CeC54fP7pTPEwtfYS5v5x+ErBWxGiB0oj1pklYGXwl/cRBw==", "game"=>{"participants_attributes"=>{"0"=>{"user_id"=>"3", "result"=>"W"}, "1"=>{"user_id"=>"2", "result"=>"W"}, "2"=>{"user_id"=>"1", "result"=>"W"}, "3"=>{"user_id"=>"6", "result"=>"W"}}, "winning_score"=>"18", "losing_score"=>"4", "notes"=>"13241234"}, "commit"=>"Submit!"}
   (0.1ms)  begin transaction
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 6], ["LIMIT", 1]]
   (0.1ms)  rollback transaction
  Rendering games/new.html.erb within layouts/application
  Rendered games/new.html.erb within layouts/application (69.4ms)
  Rendered layouts/_shim.html.erb (0.4ms)
  Rendered layouts/_header.html.erb (0.7ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 199ms (Views: 144.9ms | ActiveRecord: 0.5ms)
考拉里克

@kkulikovskis评论对我有用。我变了:

has_many :participants

has_many :participants, inverse_of: :game

在游戏模型中

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring 事务与几个操作和回滚

来自分类Dev

不在事务中时回滚Spring JDBC操作

来自分类Dev

如何在Rails中回滚数据库事务

来自分类Dev

Peewee:外部事务不会回滚内部事务(保存点)

来自分类Dev

SQL Codeigniter:创建保存点并回滚到保存点/从控制器回滚多个事务

来自分类Dev

Firestore(数据存储模式)-如果事务回滚,则记账多少写操作?

来自分类Dev

可以将第二个保存事务t2回滚吗

来自分类Dev

按下创建按钮并获取值字段以将其插入到 form_for 时回滚事务

来自分类Dev

为什么Rails会忽略(伪)嵌套事务中的回滚?

来自分类Dev

JTA事务的回滚

来自分类Dev

HibernateTransactionManager回滚事务

来自分类Dev

当前事务无法提交,并且不支持写入日志文件的操作。回滚交易

来自分类Dev

通过回滚NUnit,SQL Server和UI测试中的事务来维护数据库已知状态的正确方法

来自分类Dev

Rails:回滚错误

来自分类Dev

在Rails中回滚?

来自分类Dev

Rails:回滚错误

来自分类Dev

回滚已提交的事务

来自分类Dev

嵌套事务无法回滚

来自分类Dev

OSGi中的事务回滚

来自分类Dev

回滚后的事务提交

来自分类Dev

回滚已提交的事务

来自分类Dev

休眠中的回滚事务

来自分类Dev

容器管理的事务回滚

来自分类Dev

ActiveRecord事务不会回滚整个事务

来自分类Dev

SQL Codeigniter事务回滚多路访问回滚

来自分类Dev

Rails交易不会回滚

来自分类Dev

Rails中的ActiveRecord回滚

来自分类Dev

Rails交易不会回滚

来自分类Dev

Fixnum:Rails回滚错误