0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【ActiveRcord】ActiveRcordとは!

Last updated at Posted at 2024-06-26

ActiveRecordについて

Ruby on Railsで使われるライブラリ(ツール)の一つ。
ActiveRecordを使用することで、SQL文を直接書かずとも、わずかなアクセスコードを書くだけで、DBとやり取りができる。
言わば、RailsとModel(DB)との仲介役的な存在。
単体でも使用可能。

ActiveRecordの基本メソッド

create
User.create(name: "David", occupation: "Artist")
#発行されるSQL -> INSERT INTO users (name, occupation) VALUES ('David', 'Artist') ;
#createメソッドでインスタンスを作成する場合、オブジェクトはDBに保存される。
#newメソッドでインスタンスを作成する場合、オブジェクトはDBに保存されない。

read
User.all
#SELECT * FROM users ;

User.find_by(name: "David")
#SELECT * FROM users where name = 'Davit' limit = 1 ;
update
User.update(name: "David")
#UPDATE users SET name = 'David' ;
destroy
User.destoroy
#DELETE FROM users WHERE users.id = 1 ;

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?