[RUBY] [Rails / Uniqueness constraint] How to check model validation on the console / Uniqueness constraint for multiple columns

Status


This validation can be achieved by using "uniqueness constraint".

This time it is a record of the operation on the console launched with rails c to see if this is implemented properly.


environment


This code

app/models/permission.rb



class Permission < ApplicationRecord
  belongs_to :user
  belongs_to :task

  validates :user_id, uniqueness: { scope: :task_id }
end

By the way, this is a constraint on the application, and to be exact, it is necessary to set a constraint on the DB side as well (<a href-"https://railsguides.jp/active_record_validations.html#uniqueness:title"> Rails guide < From / a>). However, I do not understand it yet, so I will omit it this time.



Commands entered on the console

I wanted to check the uniqueness constraint, so I executed the code according to the following flow.


I launched the console in the terminal and checked.

teminal


$ rails c

[1] > permission = Permission.find(1)
=> #<Permission:*** id: 1, user_id: 1, task_id: 1, ***>

[2] > new.valid?
=> true

It has become true ... I thought about the cause and changed the command.


Cause and command change

[Cause] ▶ "find" is an ActiveRecord method that searches for existing records ▶ If you save this, will it look like an overwrite save?

【Change】 ▶ Directly type user_id and task_id


terminal


$ rails c

[1] > Permission.find(1)
=> #<Permission:*** id: 1, user_id: 1, task_id: 1, ***>

[2] > permission = Permission.new(user_id: 1, task_id: 1)
=> #<Permission:*** id: nil, user_id: 1, task_id: 1, ***>

[3] > permission.valid?
=> false

[4] > permission.errors.full_messages
=> "User has already been taken"

I was able to confirm that it was completely false.


Summary


I was a little addicted to it, but I managed to get over it. To really understand, you need to know what's working behind save and valid.


I still have a lot to study, but I will keep it down one by one.


Reference article


Recommended Posts

[Rails / Uniqueness constraint] How to check model validation on the console / Uniqueness constraint for multiple columns
How to debug the processing in the Ruby on Rails model only on the console
How to add / remove Ruby on Rails columns
How to check Rails commands in the terminal
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to change the column name
[Rails] I don't know how to use the model ...
How to write Rails validation
[Rails] How to use validation
How to run React and Rails on the same server
How to check the database of apps deployed on Heroku
How to deploy jQuery on Rails
How to specify validation for time_field
How to use Ruby on Rails
[Rails] Easy way to check columns
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] How to change the page title of the browser for each page
Rails on Tiles (how to write)
[Rails] How to put a crown mark on the ranking function
How to check for the contents of a java fixed-length string
[Ruby on Rails] How to avoid creating unnecessary routes for devise
Check the root on the Rails browser
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
How to implement search function with rails (multiple columns are also supported)
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Gorigori beginners summarized how to operate the database using rails console Ntiunus
Ruby on Rails DB Tips for creating methods to reduce the load
How to solve the local environment construction of Ruby on Rails (MAC)!
[Ruby On Rails] How to search the contents of params using include?
How to check Java installed on Mac
[Ruby on Rails] How to use CarrierWave
[Rails] How to use rails console with docker
[Rails] How to use the map method
Prepare the security check environment for Rails 6
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] How to make the link destination part of the specified id
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Rails] How to decide the destination by "rails routes"
Validation settings for Ruby on Rails login function
How to check the logs in the Docker container
[Ruby on Rails] How to display error messages
How to search multiple columns with gem ransack
[Rails] How to implement unit tests for models
[For beginners] How to implement the delete function
Model unit test code to check uniqueness constraints
How to install multiple JDKs on Ubuntu 18.04 LTS
[Rails MySQL] How to reset DB on heroku
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Java] Memo on how to write the source
[Java] How to make multiple for loops single
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
How to change the process depending on the list pressed when there are multiple ListViews
How to check the WEB application created in the PC development environment on your smartphone
[Ruby On Rails] Correct description location of unique constraint that gives uniqueness to DB
[Rails] Let's dynamically get the threshold of model validation "length check" from table information
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
I want to hit the API with Rails on multiple docker-composes set up locally