[Ruby on Rails] If there are duplicate records when registering data, do not register

background

After deploying the original app, I notice that duplicate data is registered. スクリーンショット 0002-09-27 午前9.43.35.png

As a specification of the application, I do not want to duplicate the registered records.

When I check the database, It turns out that if the user to register is different, it will be possible to register. スクリーンショット 0002-09-27 午前9.44.59.png

It took some time to resolve, so I will record it as a reminder.

Problem area

The condition for data registration was set in the controller.

require 'rubygems'
require 'mechanize'

def create
    f = (params[:wiki_url])
    
    unless 
      f.start_with?("https://ja.wikipedia.org/wiki/")
      flash[:notice] = "Could not save because an invalid URL was entered."
      redirect_to action: 'new'
      return
    else
      agent = Mechanize.new
      page = agent.get(f) 
      page.encoding='utf-8'

Omitted below...

In the unless statement, enter "https://ja.wikipedia.org/wiki/" in the entered data (URL). If is not included, the condition was set so that it would not be registered.

However, if there are duplicate records, the condition that they are not registered was not included, so The same data as shown in the picture has been registered.

Solution (conclusion)

I rewrote it as follows and solved it.

require 'rubygems'
require 'mechanize'

  def create
    f = (params[:wiki_url])

    if not 
      f.start_with?("https://ja.wikipedia.org/wiki/")
      flash[:notice] = "Could not register because an invalid URL was entered."
      redirect_to action: 'new'
      return
    elsif
      Company.where(page_url: "#{f}").count >= 1
      flash[:notice] = "Could not register because the registered URL was entered."
      redirect_to actiont: 'new'
      return
    else
      agent = Mechanize.new
      page = agent.get(f) 
      page.encoding = 'utf-8'

Omitted below...

The article on teratail was helpful and I came up with a solution. : point_right: I want to get a record in which two or more data with the same name are registered using ActiveRecord.

There are two major changes.

-Changed from unless statement to if not statement -** Where method added ** (Here is the point!)

Change from unless statement to if not statement

There is no elsif in the ruby unless statement. .. .. That's right.

I want to add a duplicate prevention condition in addition to the invalid data input prevention condition. However, since elsif cannot be used in unless statements, Added elsif instead of if not statement.

** Where method added ** (Here is the point!)

The flow of conditions (processing) is as follows.

--Get the number of records that match the conditions in the table with the where method --Judge 1 or more (whether there is data) based on the number of acquired data --If it is 1 or more (there is data), it will not be accepted.

Get the number of records that match the conditions in the table with the where method

You can get records that meet the conditions with .where ("condition").

Model name.where("conditions")

. This time, we will get the page_url column of the Company model.

Company.where(page_url)

. In addition, I want to search for data that matches the data entered in the page_url column, so Add as follows.

Company.where(page_url: "#{f}")

.

{f} represents the string in the variable.

The variable f is defined in the following place, and the contents are like this.

require 'rubygems'
require 'mechanize'

  def create
    f = (params[:wiki_url]) <=This is here.

Omitted below...

image.png

. To determine how many were found as a result of searching in the page_url column Add a count method.

Company.where(page_url: "#{f}").count

Judge 1 or more (whether there is data) by applying the number of acquired data to the condition

Check if there is 1 or more, that is, count the number of URLs that are the same as the entered data.

Company.where(page_url: "#{f}").count >= 1

If it is 1 or more (there is data), it will not be accepted.

image.png

bonus

It is reflected in the app, so please play with it. Unsung:hero image.png

Recommended Posts

[Ruby on Rails] If there are duplicate records when registering data, do not register
Ruby on Rails record search, create if not find_or_create_by method
[Rails] What to do if data is not registered in DB
What to do when Blocked Host: "host name" appears in Ruby on Rails
What to do if deployment fails on Heroku (Ruby app not detected)
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
Do not return when memoizing in Ruby
When the Ruby on Rails terminal rolls back
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] Implementation of validation that works only when the conditions are met
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
Ruby Hash stack overflows when there are many arguments
Ruby on Rails Tutorial Troublesome notes when running on Windows
What to do when Rails on Docker does not reflect controller changes in the browser