勉強会
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # ActiveRecord teamB ### ToDoリスト - ActiveRecordの条件付けを調べる - 実行 ```ruby= ConferenceInfo.find(25) ``` #引数に入っている数字と同じidのレコードを取得 ### ToDoリスト - ActiveRecordの最初のデータだけ表示する方法を調べる - 実行 ```ruby= ConferenceInfo.first ``` ### ToDoリスト - ### ToDoリスト - ActiveRecordの最後のデータだけ表示する方法を調べる - 実行 ```ruby= ConferenceInfo.last ``` ### ToDoリスト - ActiveRecordの講演会名が「テスト21講演会」のデータ表示する方法を調べる - 実行 ```ruby= ConferenceInfo.where(confName: "テスト21講演会") ``` ### ToDoリスト - ### ToDoリスト - ActiveRecordのアクセスログテーブルのデータ先頭から10個のみ取得する方法を調べる - 実行 ```ruby= ConferenceInfo.first(10) ``` first 最初からだけ limit 途中からでも取れる ### ToDoリスト - ### ToDoリスト - ActiveRecordのアクセスログテーブルの先頭から1901個目以降のデータを取得する方法を調べる - 実行 ```ruby= AccessLog.offset(1900) ``` offset ページネーションで使う(1~100で表示) ### ToDoリスト - ActiveRecordのクセスログテーブルのデータすべて表示する方法を調べる - カラムはアクセスログID、都道府県、 施設、 氏名を表示する方法を調べる - 実行 ```ruby= AccessLog.all AccessLog.select('accessLogId', 'prefecture','facility','name') ``` AccessLog.select(:accessLogId, :prefecture, :facility, :name) ### ToDoリスト - ActiveRecordのアクセスログテーブルの先頭から1901個目以降のデータを取得 - アクセスログテーブルのデータ10個のみ取得 - 実行 ```ruby= AccessLog.limit(10).offset(1900) ``` ### ToDoリスト - アクセスログテーブルの全データの数を表示の方法を調べる - 実行 ```ruby= AccessLog.count ``` ### ToDoリスト - アクセスログテーブルの集合視聴人数の平均を表示の方法を調べる - 実行 ```ruby= AccessLog.average(:participants_number) ``` ### ToDoリスト - アクセスログテーブルの集合視聴人数の最高人数を表示の方法を調べる - 実行 ```ruby= AccessLog.maximum(:participants_number) ``` ### ToDoリスト - アクセスログテーブルの集合視聴人数の最低人数を表示の方法を調べる - 実行 ```ruby= AccessLog.minimum(:participants_number) ``` ### ToDoリスト - アクセスログテーブルの集合視聴人数の合計を表示の方法を調べる - 実行 ```ruby= AccessLog.sum(:participants_number) ``` ### ToDoリスト - アクセスログテーブルの入室時間が早い順に表示 - 実行 ```ruby= AccessLog.order(accessTime: :asc) ``` ### ToDoリスト - アクセスログテーブルの入室時間が遅い順に表示 - 実行 ```ruby= AccessLog.order(accessTime: :desc) ``` ### ToDoリスト - アクセスログテーブルのデータをランダムに1件のみ表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.order("RAND()").limit(1) ``` firstが多い ルビーでは 文字列を書く時には、基本的には'シングル "ダブルのときは、エスケープ/nや、文字列と関数を合わせてかくときなど #{関数} <% %> ERBというHTMLの中にルビーがかけますよで使う(<? PHP ?>と同じ) コードが長くなる場合 ルビーでも開業できる ```ruby= AccessLog.limit(10) .offset(1900) ``` ActiveRecordはデータ型 データ型は「.class」で調べることが可能 ### ToDoリスト - アクセスログテーブルの都道府県が佐賀県のデータを表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.where("prefecture = '佐賀県'") ``` ### ToDoリスト - アクセスログテーブルの都道府県が福岡県かつ、施設が柳沢病院のデータを表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.where(prefecture: '福岡県', facility: '柳沢病院') ``` ### ToDoリスト - アクセスログテーブルの施設名が松野病院または千田病院のデータを表示アクセスログテーブルの施設名が松野病院または千田病院のデータを表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.where(facility: '松野病院').or(AccessLog.where(facility: '千田病院')) ``` ### ToDoリスト - アクセスログテーブルの施設名が松野病院または千田病院のデータを表示アクセスログテーブルの施設名が松野病院または千田病院のデータを表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.where(facility: '松野病院').or(AccessLog.where(facility: '千田病院')) ``` ### ToDoリスト - アクセスログテーブルのユニークIDが空でないデータの数を表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.where.not(uid: 'null').count ``` ### ToDoリスト - アクセスログテーブルのユーザーリストIDが18~20のデータを表示する方法を調べる - ユーザーリストIDを昇順で並べ替え - カラムは全部表示 - 実行 ```ruby= AccessLog.limit(3).offset(17).order(user_list_id: :asc) AccessLog.where(user_list_id: 18..20).order(user_list_id: :asc) ``` ### ToDoリスト - アクセスログテーブルのユーザーエージェントにfirefoxという文字が含まれるデータを表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.where('userAgent LIKE ?', '%firefox%') ``` ### ToDoリスト - アクセスログテーブルの集合視聴人数が25人以上のデータを表示する方法を調べる - カラムは全部表示 - 実行 ```ruby= AccessLog.where(participants_number: 25..) ``` ### ToDoリスト - アクセスログテーブルのIPアドレスが「2.181.230.81」のデータを表示を調べる - カラムの指定方法をを調べる - カラムはアクセスログID、都道府県、施設、氏名、IPアドレス - 実行 ```ruby= AccessLog.select(:accessLogId, :prefecture, :facility, :name, :hostIP) .where(hostIP: '2.181.230.81') ``` ### ToDoリスト - アクセスログテーブルの都道府県ごとの集合視聴人数(participants_number)を集計 - 都道府県ごとの参加人数が500人以上のレコードのみを表示 - 都道府県がnullのレコードは表示なし - 表示はカラム名に都道府県名、値に集合視聴人数の合計を表示 - 実行 ```ruby= AccessLog.group(:prefecture)      .sum(:participants_number)      .where("participants_number >= ?" , "500")      .where.not(prefecture: nil) .select(:prefecture, :participants_number) AccessLog.group(:prefecture) .having('sum(participants_number) > ? && prefecture != ?', 500, nil) .select(:prefecture, :participants_number) ``` ④-3 distinctの引数にはカラムは入らない ture,falseのみ ※カラム名を書けない ### 4/12 ### ToDoリスト - LEFT JOINをActiveRecordでやる方法を調べる - アクセスログテーブルのアクセスログID「46」に紐づいているアンケートログテーブルのデータを表示 - アンケートログテーブルのカラムをすべて表示 - 実行 ```ruby= AccessLog.left_joins(:enquete_answer)       .where(accessLogId: 46) ``` ### ToDoリスト - 方法を調べる - カンファレンスインフォテーブルのカンファレンスインフォID「39」のカンファレンスインフォID、講演会名、講演会日時を表示 - カンファレンスインフォID「39」に紐づいているアクセスログテーブルから都道府県、施設、氏名を表示 - 実行 ```ruby= ConferenceInfo.left_joins(:access_log)       .select("conference_info.conferenceInfoId") ConferenceInfo.joins(:access_log) .select("conference_infos.conferenceInfoId, conference_infos.confName, conference_infos.conferenceDate, access_logs.prefecture, access_logs.facility, access_logs.name") .where(conferenceInfoId: 39) ``` ### ToDoリスト - 方法を調べる - アクセスログテーブルのカンファレンスインフォIDが「3」のデータに紐づけられているアンケートログテーブルのデータを取得 - カラムはアクセスログテーブルのアクセスログID、都道府県、氏名、アンケートログテーブルの問1の答え、問2の答えを取得 - アンケートログテーブルにデータが無い場合はそのレコードを表示しない - 実行 ```ruby= AccessLog.left_outer_joins(:enquete_answer) .where(conference_info_id: 3) .select( :accessLogId, :prefecture, :name, :answer1, :answer2 ) ``` ### ToDoリスト(5-4) - 方法を調べる - カンファレンスインフォテーブルの講演会名「テスト14講演会」のデータを出力 - カンファレンスインフォテーブルに紐づいているユーザーIDリストテーブルのデータも出力 - カンファレンスインフォテーブルのカラムは全て表示、ユーザーIDリストテーブルのカラムはログインIDとパスワードのみ表示 - SQLのクエリ回数を減らすために今回の取得したテーブルはキャッシュしておく - 実行 ```ruby= ConferenceInfo.where(confName: "テスト14講演会") .join(:user_list) .select( :'conference_infos.*', :userID, :userPassword ) Rails.cache.fetch(<cache_key>, expires_in: 3.hours) do ConferenceInfo.eager_load(:user_list).where(ConferenceInfo: {confName: "テスト14講演会"}) UserList.eager_load(:conference_info) .where(conference_infos: {conferenceInfoId: 14}) .select( :conferenceInfoId, :confName, :conferenceDate, :created_at, :updated_at, :userID, :userPassword ) conferenceInfoId confName conferenceDate created_at updated_at ``` ### ToDoリスト(5-5) - 方法を調べる - カンファレンスインフォテーブルのカンファレンスインフォID「47」のカンファレンスインフォID、講演会名、講演会日時を表示 - カンファレンスインフォID「47」に紐づいているアクセスログテーブルから都道府県、施設、氏名を表示 - カンファレンスインフォID「47」に紐づいているユーザーIDリストからログインIDとパスワードを表示 - 実行 has_many 子供 belongs_to 親 ※親と子の関係注意!! ```ruby= ConferenceInfo.left_outer_joins(:access_log, :user_list) .where(conference_infos: {conferenceInfoId: 47}) .select( :conferenceInfoId, :confName, :conferenceDate, :prefecture, :facility, :name, :userID, :userPassword ) ``` ### ToDoリスト(5-6) - ・方法を調べる - ・カンファレンスインフォテーブルのカンファレンスインフォID「21」のカンファレンスインフォID、講演会名、講演会日時を表示 - ・カンファレンスインフォID「21」に紐づいているアクセスログテーブルから都道府県、施設、氏名を表示 - ・アクセスログテーブルのに紐づいているアンケートログテーブルから問1の答えと問2の答えを表示 - ・実行 ```ruby= ConferenceInfo.left_outer_joins(:access_log, :enquete_answer) .where(conference_infos: {conferenceInfoId: 21}) .select( :conferenceInfoId, :confName, :conferenceDate, :prefecture, :facility, :name, :answer1, :answer2 ) ``` ### ToDoリスト(6-1) - ・BETWEENをアクティブレコードでやる方法を調べる - ・アクセスログの2023年2月6日の14:00~14:15の間にアクセスされた、IP(202.237.113.26)でChromeを使って視聴している方のログインIDとパスワードを表示 - ・カラムはアクセスログIDとログインIDとパスワードを表示 - ・実行 ```ruby= AccessLog.left_outer_joins(:user_list) .where(accessTime: '2023-02-06 14:00'..'2023-02-06 14:15') .where('userAgent LIKE ? OR hostIP LIKE ?', '%Chrome%', '%202.237.113.26%') .select(:userID, :userPassword) .where(accessTime: ('2023-02-06 14:00'..'2023-02-06 14:15'), hostIP: '202.237.113.26') ``` belongs_toとはその子が私の親です ### ToDoリスト(6-2) - ・講演会の視聴者数ランキングを1位(多い方)から表示 - ・カラムは講演会IDと視聴者数合計値を出力 - ・実行 participants_number ```ruby= AccessLog.group(:conference_info_id) .order('sum(participants_number) desc') .select("conference_info_id, sum(participants_number)") ``` SQLを使う時には文字列なのでクォーテーションがいる(シングルでもダブルでもOK) .select("conference_info_id, sum(participants_number)") ### ToDoリスト(6-3) - ・講演会の視聴者数ランキング1位(多い方)のアクセスログを全て表示 - ・実行 participants_number ```ruby= ConferenceInfo.joins(:access_log) .group(:conferenceInfoId) .order('SUM(participants_number) DESC') .select('conference_infos.*, access_logs.*').limit(1) .access_log .select('conference_infos.*, access_logs.*') ConferenceInfo.find_by(confName: 'テスト29講演会') .access_log .where('userAgent NOT LIKE ?', '%Firefox%') .where('entryPage LIKE ?', '%live%') .pluck(:hostIP) .uniq.count ``` ### ToDoリスト(6-4) - ・入室時間が「2023-02-08」でライブ配信のアクセスログを表示 - ・参加登録の都道府県がnullの物は表示しない - ・参加登録の都道府県ごとに見たとき、アンケートログの問1の答えが「1」と回答された数が「2」以上の都道府県を表示 - ・カラムはアクセスログテーブルの「アクセスログID(nullでok)」「都道府県」とアンケートログテーブルの 「都道府県ごとの問1の答えの数」を表示 - ・実行 ```ruby= AccessLog.joins(:enquete_answer) .group(:prefecture) .where(accessTime: '2023-02-08 00:00'..'2023-02-09 00:00') .where('entryPage LIKE ?','%live%') .where.not(prefecture: nil) .having(enquete_answers:{answer1: 1}) .count(:answer1) .where(enquete_answers:{answer1: 1}) .select( :answer1 ) AccessLog.joins(:enquete_answer) .group(:prefecture) .where(accessTime: '2023-02-08 00:00'..'2023-02-09 00:00') .where('entryPage LIKE ?','%live%') .where(enquete_answers:{answer1:1}) .where.not(prefecture: nil) .having('count(enquete_answers.answer1) : 2..') .select( :prefecture, :answer1 ) .where(enquete_answers:{answer1:1}).count .having('count(enquete_answers:{answer1:1}) > ?', 2) .having('sum(participants_number) > ? && prefecture != ?', 500, nil) AccessLog.joins(:enquete_answer) .where("access_logs.accessTime > '2023-02-08 00:00'") .where("access_logs.entryPage LIKE '%live%'") .where.not(access_logs: {prefecture: nil}) .where(enquete_answers: {answer1: 1}) .group("access_logs.prefecture") .having("count(answer1) >= 2") .select('access_logs.prefecture, count(enquete_answers.answer1) as count_answer') ``` ### ToDoリスト(7-1) - ・ActiveRecordのレコードの追加方法を調べる - ・データ作成者にチーム名を入力(※ 削除する際に使用するので次の問題以降も同じ名前) - ・講演会名に「班員の誰かの名前 + 講演会」 を入力 - ・その他のカラムはNullとする - ・実行 上記条件を満たすActiveRecord文を記載 ```ruby= SqlPlacticeLog.create(created_by: "チームB", conf_name: "山北講演会") ``` ### ToDoリスト(7-2) - ・ActiveRecordのレコードの更新方法を調べる - ・問題1で作成したレコードを更新 - ・講演会情報に適当な文を入力 - ・実行 ```ruby= SqlPlacticeLog= SqlPlacticeLog.find(96) SqlPlacticeLog.update(conf_info: "適当な文!!") ``` ※型の指定をしたいとき ストリング型にするときは「.to_s」 https://pikawaka.com/ruby/to_s ### ToDoリスト(7-2) - ・データ作成者にチーム名を入力1件目、2件目同様 - ・講演会日に1件目は本日の日付、2件目は明日の日付 - ・講演会時間に1件目は現在時刻、2件目は現在時刻を明日の日付にしたもの - ・その他のカラムはNullとする - ・実行 ```ruby= SqlPlacticeLog.create(created_by: "チームB", conf_date: today, conf_time: now) SqlPlacticeLog.create(created_by: "チームB", conf_date: tomorrow, conf_time: ) ``` https://qiita.com/TerToEer_sho/items/4b0e8f3ff9ddda600b36 http://www.openspc2.org/reibun/Ruby/time/007/index.html

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully