質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Active Record

Active Recordは、一つのオブジェクトに対しドメインのロジックとストレージの抽象性を結合するデザインパターンです。

Q&A

0回答

1445閲覧

Rails 複数カラムのデータを検索したい。記述が不明。

masaosan18

総合スコア65

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Active Record

Active Recordは、一つのオブジェクトに対しドメインのロジックとストレージの抽象性を結合するデザインパターンです。

0グッド

0クリップ

投稿2020/09/23 06:39

編集2020/09/24 07:41

やりたいこと

Officeテーブルにあるカラムでフリーキーワード検索したいと考えています。
複数カラムキーワード検索したいのですが、

  • name
  • address
  • near_station
  • introduction
  • company

これらのカラムで入力されたキーワードに該当するOfficeを取得したいです。
Officesコントローラーのsearchアクションの記述の方法がわかりません。
詳しい方、ご教授お願い致します。

コード

class OfficesController < ApplicationController include Pagy::Backend def index pagy, offices = pagy(Office.all) pagy_headers_merge(pagy) render json: offices, each_serializer: OfficeIndexSerializer, include: '**' end def show office = Office.find(params[:id]) render json: office, serializer: OfficeShowSerializer, include: '**' end def search if params[:city_id] offices = Office.where(city_id: params[:city_id]) elsif params[:name, :near_station] offices = Office.where('name LIKE ?', "%#{params[:name]}%", 'address LIKE ?', "%#{params[:address]}%", 'near_station LIKE ?', "%#{params[:near_station]}%", 'introduction LIKE ?', "%#{params[:introduction]}%", 'company LIKE ?', "%#{params[:company]}%" ) else pagy, offices = pagy(Office.all) end render json: offices end end
# frozen_string_literal: true Rails.application.routes.draw do get '/offices', to:'offices#index' get '/offices/search', to:'offices#search' # get '/offices/keyword', to:'offices#keyword' get '/offices/:id', to:'offices#show' get 'cities', to:'cities#get' mount_devise_token_auth_for 'User', controllers: { registrations: 'users' } devise_scope :user do get 'signin' => 'devise_token_auth/sessions#new' post 'signin' => 'devise_token_auth/sessions#create' post 'signup' => 'users#create' put 'update' => 'users#update' end get 'users/bookmarks', to: 'bookmarks#index' post '/users/bookmarks', to: 'bookmarks#create' delete '/users/bookmarks', to: 'bookmarks#destroy' get 'users/thanks/new', to: 'thanks#new' get 'users/thanks', to: 'thanks#index' post '/users/thanks', to: 'thanks#create' delete '/users/thanks', to: 'thanks#destroy' put '/users/thanks', to: 'thanks#update' #予約している日付の一覧を返す get '/reserve', to: 'reservations#index' #予約を作成する post '/reserve', to: 'reservations#create' # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end
# frozen_string_literal: true class CreateOffices < ActiveRecord::Migration[6.0] def change create_table :offices do |t| t.string :login_id t.string :password t.string :name t.string :title t.string :postcode t.string :address t.string :near_station t.string :tel t.string :fax t.string :email t.string :office_type t.string :opening_date t.string :room_number t.string :requirement t.string :facility t.string :company t.text :url t.text :introduction t.integer :staff_number t.references :city, foreign_key: true t.timestamps end end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問