[RUBY] Rasils data save, update, delete

Purpose of this post -Minutes about Rails controller operations ((1) save, (2) update, (3) delete).

Teaching materials used for learning Udemy ["Introduction to Ruby on Rails for the First Time-Learn Ruby and Rails from the Basics and Publish Web Applications to the Net"](https://www.udemy.com/course/the-ultimate-ruby-on-rails- I used bootcamp /) as a teaching material.

① Save (create) -Reflect the newly created value in db and save it

qiita.rb


 def create
    @instance=model.new(Key 1:Value 1,Key 2:Value 2)
    @instance.save
  end

[Explanation] ○ @ Instance = Model.new (Key 1: Value 1, Key 2: Value 2) ⇨ Store the value in db via the instance variable

○ @ Instance.save ⇨ ⇨ Save instance variables

2 update -Update the value already saved in db ・ (I want to update from value 1 → value 1_1, value 2 → value 2_2)

qiita.rb


  def update
    @instance=model.find(params[:id])
    @instance.update(Key 1:Value 1_2,Key 2:Value 2_2)
  end

[Explanation] ○ @ Instance = Model.new (params [: id]) ⇨ Specify the key as an argument and store the hash value of db in the instance.

○ @ Instance.update ⇨ ⇨ Update instance variables

③ Delete (destroy) -Delete the value already saved in db

qiita.rb


  def destroy
    @instance=model.find(params[:id])
    @instance.destroy
  end

[Explanation] ○ @ Instance.destroy ⇨ Delete all db cache data

About CRUD ・ How to read is clad ・ Generic term for Create, Read, Update, Delete ・ C, U, D correspond to those explained in this article. ・ (C = ①create, U = ②update, D = ③destroy) ・ For R, index action and show action are equivalent. ・ R does not change the value of db, so it will not be explained in detail here.

Recommended Posts

Rasils data save, update, delete
Ruby: Update CouchDB data (Update)
Ruby: Delete CouchDB data
JPA's OneToMany delete becomes update
[Rails] Various ways to delete data