[Ruby On Rails] How to search and save the data of the parent table from the child table

About this article

It is a memorandum.

Use the create action and include? Method on the child controller to search the data in the parent table, and if the same content as the searched data is included, save it in the column of the child table.

What is the include? method?

Use the include? Method to see if it contains data such as a number or string.

For example

Sample code


  array = [aiueo, yayuyo]

  puts array.include?(aiueo)
  puts array.include?(yayuyo)
  puts array.include?(wawon)

Execution result


  true
  true
  false

And will be.

Premise

① Parents: parents table

id word
1 AIUEO

Child: kids table (no data yet registered)

id text word_id

② It is assumed that word_id is included in @ kid's params through [Parent's view]-> [Child's view]-> [new action (data registration screen)].

Main subject

This time we will search the contents of the "word" column of the parent table to see if it matches the data entered by the user. If they match, save. When saving the data, I want to determine whether the column information of the parent table is included, so add an arbitrary method name to the if statement. This time, let's say having_word ?.

I will write a method that actually processes.

① Define having_word ?.

kids_controller.rb


  def having_word?

  end

(2) Check whether specific data is included in the data (@kid) entered by the user (include?).

kids_controller.rb


  def having_word?
    @kid.include?
  end

③ Provide a column for writing "how to check specific data". → Write () immediately after include ?.

kids_controller.rb


  def having_word?
    @kid.include?()
  end

④ Describe @kid to refer to the params up to the create action.

kids_controller.rb


  def having_word?
    @kid.include?(@kid)
  end

⑤ Get parent (parents table) information included in params

kids_controller.rb


  def having_word?
    @kid.include?(@kid.parent)
  end

⑥ Extract the word column from the parent information acquired in "⑤".

kids_controller.rb


  def having_word?
    @kid.include?(@kid.parent.word)
  end

In the parentheses of include ?, I think it's like going back from the child to the parent's information.

Example table when the create action is executed and the data is saved

Parents: parents table

id word
1 AIUEO
2 energy
3 Run
4 english

Child: kids table

id text word_id
1 AIUEO 1
2 Aiueo order 1
3 Aiueo table 1
4 Aiueo composition 1
5 How are you? 2
6 I'm really fine. 2
7 Self-propelled power 3
8 Mileage 3
9 I have studied english for 5 years. 4
10 just a random english comment passing by. 4

Finally

Since I am a beginner in programming, please point out any points that cannot be reached. I thought that it is necessary to understand and specify the problem that the validation error message is not displayed in the future.

Recommended Posts

[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby On Rails] How to search the contents of params using include?
How to solve the local environment construction of Ruby on Rails (MAC)!
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
[Ruby on Rails] How to make the link destination part of the specified id
[Ruby on Rails] How to change the column name
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Rails] How to change the column name of the table
How to make a unique combination of data in the rails intermediate table
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
(Ruby on Rails6) How to create models and tables
[Rails] How to search across columns of related models (parent or child) in ransack
[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
How to run React and Rails on the same server
How to use Ruby on Rails
[Rails] How to get the URL of the transition source and redirect
[Ruby on Rails] Change the save destination of gem refile * Note
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to delete large amounts of data in Rails and concerns
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
How to get and add data from Firebase Firestore in Ruby
Things to remember and concepts in the Ruby on Rails tutorial
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] How to use CarrierWave
part of the syntax of ruby ​​on rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to change the maximum and maximum number of POST data in Spark
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
[Rails] How to create a table, add a column, and change the column type
How to debug the processing in the Ruby on Rails model only on the console
[Rails] How to solve the time lag of created_at after save method
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to find the cause of the Ruby error
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Ruby on Rails] How to display error messages
[Ruby on Rails] Until the introduction of RSpec
How to add / remove Ruby on Rails columns
Ruby on Rails ~ Basics of MVC and Router ~
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] Change the update date and creation date to your favorite notation
How to resolve errors that occur in the "Ruby on Rails" integration test
From the introduction of devise to the creation of the users table
[Ruby on Rails] How to write enum in Japanese
How to write Scala from the perspective of Java
Docker the development environment of Ruby on Rails project
[Ruby] How to find the sum of each digit
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby On Rails] How to reset DB in Heroku
[Rails] How to get the contents of strong parameters
[Ruby] How to get the tens place and the ones place
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) Reflecting the posted content from the form
Try using the query attribute of Ruby on Rails
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Library not loaded when trying to upgrade the version of ruby and rails s
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails