如何在创建动作中使用find_or_create?

时间:2016-03-31 12:36:42

标签: ruby-on-rails ruby-on-rails-4

我想在我的游戏日期控制器中使用find_or_create方法。当params在game_date_params中时,我不知道如何在create action中使用该方法。任何建议如何从game_date_params中提取日期?

class GameDatesController < ApplicationController
  before_action :authenticate_user!
  before_action :authenticate_admin

  def index
    @game_dates = GameDate.all
    @showcases  = Showcase.joins(:game_dates)
  end

  def new
    @game_date = GameDate.new
    @game_date.referee_stats.build
  end

  def create
    @game_date = GameDate.new(game_date_params)
    if @game_date.save
      redirect_to showcases_path
      flash[:success] = "Game date created"
    else
      render 'new'
    end
  end

  def show
    @game_date = GameDate.find(params[:id])
  end

  def destroy
    @game_date = GameDate.find(params[:id]).destroy
    redirect_to root_url
    flash[:success] = "Game date deleted"
  end


  private

  def game_date_params
    params.require(:game_date).permit(:date, referee_stats_attributes: [ :games_count, :showcase_id ])
  end

这是POST动作的输出:

  

在2016-04-01 10:21:44 +0200开始发布“/ game_dates”for 127.0.0.1   GameDatesController处理#create as HTML参数:   { “UTF8”=&gt; “中✓”,   “authenticity_token”=&gt; “中jmuOmMCO / WTFIkxrsw5l2cPVMqZAl7h11f281I + OyoHH3ddwKoB9ANAqvQEHulR88c7fzQXnnIaxs8FChMCjqw ==”,   “game_date”=&gt; {“date(1i)”=&gt;“2016”,“date(2i)”=&gt;“4”,“date(3i)”=&gt;“1”,   “referee_stats_attributes”=&GT; { “0”=&GT; { “games_count”=&gt; “中4”,   “showcase_id”=&gt;“1”}}},“commit”=&gt;“创建游戏日期”}用户加载   (0.5ms)SELECT“users”。* FROM“users”WHERE“users”。“id”= $ 1 ORDER   BY“users”。“id”ASC LIMIT 1 [[“id”,1]] GameDate Load(0.4ms)   选择“game_dates”。* FROM“game_dates”WHERE“game_dates”。“date”IS   NULL LIMIT 1(0.2ms)BEGIN SQL(0.4ms)INSERT INTO“game_dates”   (“date”,“created_at”,“updated_at”)VALUES($ 1,$ 2,$ 3)返回   “id”[[“date”,“2016-04-01”],[“created_at”,“2016-04-01   08:21:44.864669“],[”updated_at“,”2016-04-01 08:21:44.864669“]] SQL   (0.4ms)INSERT INTO“referee_stats”(“games_count”,“showcase_id”,   “game_date_id”,“created_at”,“updated_at”)价值(1美元,2美元,3美元,4美元,   $ 5)返回“id”[[“games_count”,4],[“showcase_id”,1],   [“game_date_id”,7],[“created_at”,“2016-04-01 08:21:44.866897”],   [“updated_at”,“2016-04-01 08:21:44.866897”]](18.1ms)COMMIT   重定向到http://localhost:3000/showcases已完成302找到   31毫秒(ActiveRecord:20.1ms)

3 个答案:

答案 0 :(得分:1)

应该是这样的:

def create
    @game_date = GameDate.find_or_create_by(game_date_params)
    if @game_date.present?
      redirect_to showcases_path
      flash[:success] = "Game date created"
    else
      render 'new'
    end
 end

答案 1 :(得分:1)

GameDate.find_or_create_by(game_date_params)会找到所有 game_date_params的记录,因此您可以通过找到date这样的特定参数来完成此操作,并通过以下方式为其分配其余属性块。例如:

def create
  # find or initialize the record
  @game_date = GameDate.find_or_initalize_by(date: game_date_params[:date]) do |game_date|
    # Accept nested attributes as well
    game_date.assign_attributes(game_date_params)
  end

  if @game_date.save
    redirect_to showcases_path
    flash[:success] = "Game date created"
  else
    render 'new'
  end
end

另请参阅:find_or_create_byAttributesAssignment API文档

答案 2 :(得分:0)

创建操作用于在调用新操作后创建对象,如果相同但更新操作用于编辑。混合创造&amp;更新逻辑我不是个好主意。也许你应该重新考虑你在意见中想做什么。