[RAILS] Ruby build method

build method

A method used when you want to create a new instance of a child model that belongs to the parent model. (Parent model and child model have association settings) An instance can be created with a value in the foreign key.

If you want to create a memo instance associated with a user

note.controller.rb


 @note = current_user.notes.build(note_params)  ##Parent model.Child model.Format as build

Contents of the generated comment instance

 @note
 id: nil,
 title: "aaa",
 user_id: 2,
 category_id: 1,
 explanation: "ppppp",
 created_at: nil,
 updated_at: nil 

The user_id contains a value.

Recommended Posts

Ruby build method
[Ruby] slice method
[Ruby] end_with? method
[Ruby] initialize method
Ruby accessor method
ruby map method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
definition of ruby method
[Ruby] Method definition summary
Method
Integer check method with ruby
Ruby algorithm (inject, method definition)
[Ruby] Notes on gets method
[Ruby] Method that returns truth
Ruby ① Build a Windows environment
[ruby] Method call with argument
Ruby design pattern template method pattern memo
[Ruby] Method to count specific characters
[Ruby] present/blank method and postfix if.
[Ruby] Extracting elements with slice method
[Ruby basics] split method and to_s method
[Ruby] How to use any? Method
String output method memo in Ruby
[Ruby] Search problem using index method
[Ruby on Rails] Convenient helper method
[Ruby] undefined method `dark?'occurs in rqr_code
How to use Ruby inject method
Ruby learning 4
[Ruby] Array
[Ruby] Obtaining even values ​​using the even? Method
to_i method
Ruby basics
Ruby learning 5
Ruby basics
java (method)
Ruby Review 2
getRequestDispatcher () method
Ruby addition
merge method
Refactoring Ruby
Ruby on Rails installation method [Mac edition]
[Ruby] From the basics to the inject method
Ruby learning 3
Implemented "Floyd Cycle Detection Method" in Ruby
Map method
include method
Abstract method
Ruby print puts p printf output method
initialize method
List method
puts method
ruby Exercise Memo II (variable and method)
Java method
Class method
Ruby problem ⑦
save! method
getParameter method
Ruby learning 2