Ruby on rails 检查单词是否在db中,如果在db中,只需为其创建定义即可

Ruby on rails 检查单词是否在db中,如果在db中,只需为其创建定义即可,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个字典应用程序,其中word有许多定义,定义属于word。这是我的文字模型 class Word < ApplicationRecord belongs_to :user has_many :definitions, dependent: :destroy accepts_nested_attributes_for :definitions self.per_page = 4 end 我的话 class WordsController < ApplicationCon

我有一个字典应用程序,其中word有许多定义,定义属于word。这是我的文字模型

class Word < ApplicationRecord

belongs_to :user
has_many :definitions, dependent: :destroy

accepts_nested_attributes_for :definitions

self.per_page = 4

end
我的话

class WordsController < ApplicationController
 before_action :set_word, only: [:show, :edit, :update, :destroy]
 before_action :authenticate_user!

# GET /words
# GET /words.json

def index
 @words = Word.all.order('created_at DESC').paginate(:page => 
 params[:page])
end

# GET /words/1
# GET /words/1.json
def show
 @definitions = @word.definitions.order(cached_votes_up: :desc, 
 created_at: :desc)
end

# GET /words/new
def new
 @word = Word.new
end

def all_new

 @word = Word.new
 1.times { @word.definitions.build }
 @words = Word.all

end

# GET /words/1/edit
def edit
end

# POST /words
# POST /words.json
def create

 @word = Word.find_or_create_by(word: word_params[:word]) do |word|
  word.definition.attributes = word_params[:definition_attributes]      
 end

 @word.user_id = current_user.id

 @word.definitions.each  {|definition| definition.user_id = 
 current_user.id }

respond_to do |format|
  if Word.where(word: word_params[:word]).present?
    format.html { redirect_to root_path, notice: 'Word already exists.' 
}
  elsif @word.save
    format.html { redirect_to root_path, notice: 'Word was successfully 
created.' }
    format.json { render :show, status: :created, location: @word }
  else
    format.html { render :new }
    format.json { render json: @word.errors, status: 
:unprocessable_entity }
  end
end
end

# PATCH/PUT /words/1
# PATCH/PUT /words/1.json
def update
respond_to do |format|
  if @word.update(word_params)
    format.html { redirect_to root_path, notice: 'Word was successfully 
updated.' }
    format.json { render :show, status: :ok, location: @word }
  else
    format.html { render :edit }
    format.json { render json: @word.errors, status: 
:unprocessable_entity }
  end
end
end

# DELETE /words/1
# DELETE /words/1.json
def destroy
@word.destroy
respond_to do |format|
  format.html { redirect_to words_url, notice: 'Word was successfully 
destroyed.' }
  format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_word
  @word = Word.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white 
list through.
def word_params
  params.require(:word).permit(:word, definitions_attributes: 
[:definition, :URL])
end
end
如何重写此代码以使其检查单词是否存在。如果没有,创建单词或单词和定义这部分工作;如果有,只创建现有单词的定义。这部分不起作用。我运行了Rails.logger.debug,看起来该块甚至没有执行。显然,在这里使用find_或create_by不是正确的方法

This is my form
<%= form_for @word do |form| %>

<% if word.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(word.errors.count, "error") %> prohibited this word 
from being saved:</h2>

  <ul>
  <% word.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<% end %>


<div class="form-group upper-margin">
 <%= form.text_field :word, id: :word_word, class: "form-control", 
 placeholder: "Word" %>
</div>

<%= form.fields_for :definitions do |definition| %>


 <div class="form-group">

  <button type="button" id="ShiftButton" class="btn btn-secondary form-
control" >I don't know the definition!</button>

  <%= definition.text_field :definition, class: "form-control 
definition", placeholder: "Definition", id: "chartdiv"%>

 </div>

<% end %>

<div class="form-group">
 <%= form.submit class: "btn btn-secondary form-control" %>
</div>

<% end %>

尽管最终可能会出现重复的定义,因为您实际上没有定义使定义唯一的方法

,而且您可以跳过第二行,并为其添加一个accepts\u nested\u attributes\u
@word = Word.find_or_create_by(word_params[:word])
@word.definitions.create(word_params[:definitions_attributes])