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?

More than 1 year has passed since last update.

Rails Model:テーブルからデータを抽出する

Last updated at Posted at 2023-04-13

環境

Tool Version
MacOS(Ventura) 13.0
Docker
Ruby 2.6.4
Rails 6.0

操作

Dockerコンテナを起動し、Webコンテナにログインする

Dockerコンテナを起動する
$ docker-compose up -d

Dockerコンテナを起動後にWebコンテナにログインする

Webコンテナにログイン
$ docker-compose exec web bash

rails consoleでRailsアプリケーションの対話モードに入る

対話モードへ
$ rails c

ORMのコマンドでモデルを操作する

モデルのテーブルのカラムを抽出する例
$ StaffMember.all

ActiveRecordで生SQLを使いたいとき

複雑なクエリを書く際には積極活用したい

ActiveRecord::Base.connectionメソッドを使う
$ ActiveRecord::Base.connection.select_all("select * from staff_members where email ilike '%@google.com'").to_hash
# "@google.com"ドメインのみを抽出
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?