After deploying the original app, I notice that duplicate data is registered.
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.
It took some time to resolve, so I will record it as a reminder.
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.
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!)
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.
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.
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}")
.
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...
. 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
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
It is reflected in the app, so please play with it. Unsung:hero
Recommended Posts