
次のようにコマンドを実行して、migrateファイルを作成します。
rails g model user name:string description:text icon:binary
rails g model hashmodel title:string content:text vote:integer favorite:integer views:integer user_id:integer
#=> モデル名"hash"だと既にRubyのクラスとしてhashがあるので、エラーになる。
rails g model hashmodel title:string content:text vote:integer favorite:integer views:integer user_id:references #=> 多分間違い。
rails g model hashmodel title:string content:text vote:integer favorite:integer views:integer user:references
#=> `user_id:references`({一対多の一モデル名}_id)とすることで、後述の`belongs_to :user`を追記する手間が省ける。それだけ。
次のコマンドで、テーブルを作成します。
rake db:migrate
belongs_to
で”一”モデルへ関連付けします。
models/hashmodel.rb
class Hashmodel < ApplicationRecord
belongs_to :user
end
has_many
で”多”モデルへ関連付けします。
models/user.rb
class User < ApplicationRecord
has_many :hashmodels, dependent: :destroy
end
dependent: :destroy
があることで、ユーザーAを削除すると、自動的にユーザーAが作成したhashmodelも全て削除されるようになります。
まぁ、一対多のテンプレみたいなものですね。
これでテーブルの作成及び、一対多の関連付けは完了です。
一対多の関連付けをした状態で色々やってみる。

まずユーザーを作成。
@user = User.create(name: "yuis", description: "hello. I'm yuis")
そのユーザーでコンテンツを作成。
@hashmodel = @user.hashmodels.create(title: "title." ,content: "content.")
できた。
新規作成したユーザーが新規作成のコンテンツを作ることもできる。
@hashmodel = User.create(name: "nanashi").hashmodels.create(title: "title." ,content: "content.")
データベースの中身を確認してみよう。
では、データベースの中身を確認してみましょう。
ちゃんとhashmodel
のuser_id
が二人分になっているかがキモですね。(一対多の関係)
まず、usersテーブルを確認してみましょう。
User.all
User Load (0.0ms) SELECT "users".* FROM "users"
+----+---------+----------------+------+-------------------------+-------------------------+
| id | name | description | icon | created_at | updated_at |
+----+---------+----------------+------+-------------------------+-------------------------+
| 1 | yuisice | よろしくです。 | | 2018-04-28 10:09:30 UTC | 2018-04-28 10:11:40 UTC |
| 2 | nanashi | | | 2018-04-28 10:14:47 UTC | 2018-04-28 10:14:47 UTC |
+----+---------+----------------+------+-------------------------+-------------------------+
2 rows in set
ちゃんと出来てますね。
では、hashmodelsテーブル。
Hashmodel.all
Hashmodel Load (0.5ms) SELECT "hashmodels".* FROM "hashmodels"
+----+----------+------------+------+----------+-------+---------+-------------------------+-------------------------+
| id | title | content | vote | favorite | views | user_id | created_at | updated_at |
+----+----------+------------+------+----------+-------+---------+-------------------------+-------------------------+
| 1 | title. | content. | | | | 1 | 2018-04-28 10:12:29 UTC | 2018-04-28 10:12:29 UTC |
| 2 | title._2 | content._2 | | | | 1 | 2018-04-28 10:13:31 UTC | 2018-04-28 10:13:31 UTC |
| 3 | title. | content. | | | | 2 | 2018-04-28 10:14:47 UTC | 2018-04-28 10:14:47 UTC |
+----+----------+------------+------+----------+-------+---------+-------------------------+-------------------------+
3 rows in set
出来てますね!
この記事で欲しい情報が見つからなかった方へ
この記事の更新情報がある場合があります。
この記事は以下の記事とも関連性があると思われます。
Ubuntu 16.04 Rails 5にpostgresSQLをセットアップ
RailsのpostgreSQLにローカルネットワーク(LAN/外部)から接続したい
Rails 5 現在のビューの絶対パス/相対パスを出力
Rails 5 Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Rails Ahoy
Railsで挫折しないためのRailsの学習の進め方
Rails ポート3000以外でサーバーを実行`rails s`したい
管理人の方針で、既存の記事の編集はせず、新しい情報や大きな既存情報の更新があった場合には、新しい記事として公開することになっています。
当記事で欲しい情報が見つからなかった・解決に至らなかった場合、これらのリンクがお役に立てるかも知れません。
どうやってこの関連記事を生成しているの?
この機能のPHPソースコード(2500行)