How to use rails join

Join multiple tables

① Model(Singular).joins(:② Model(Multiple))

① is User
②Schedule

Prerequisites

join joins with a common term

Only records whose user_id and User id in the Schedule table ** match ** are acquired. Records that do not match the join condition are deleted to form one table.

How to get columns

Get only user column
User.joins(:schedules)
Get both columns
User.joins(:schedules).select("users.*, schedules.*")

Supplement

When using methods such as where

where(Model name(Plural form):{column:value})

Is it a User model? Alternatively, you need to specify whether it is a schedule model

Caution

All the values obtained by select are changed to ** string **. In my case, because datetime was changed to a string, I couldn't use the time method, and I used time.parse to change from a string type to a date type.

at the end

This is my first post. I'm sorry that it's hard to understand. I hope it helps someone even a little.

Recommended Posts

How to use rails join
[Rails] How to use enum
[Rails] How to use enum
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Map
How to use custom helpers in rails
How to write Rails
How to use rbenv
[Ruby on Rails] How to use CarrierWave
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
[Rails] How to use rails console with docker
How to use map
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use collection_select
How to use active_hash! !!
How to use MapStruct
How to use MySQL in Rails tutorial
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
How to use Dozer.mapper
How to use Gradle
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Map
[rails] How to post images
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
How to use java Optional
How to use JUnit (beginner)