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 3 years have passed since last update.

railsチュートリアル第13章 User/Micropostの関連付け

Posted at

###User/Micropostの関連付け

####マイクロポストがユーザーに所属する関連付け
app/models/micropost.rb

class Micropost < ApplicationRecord
# ApplicationRecordを継承したモデルが作られます
  belongs_to :user
  # userモデルとの間に「1対1」のつながりが設定
  # 割り当てる
  validates :user_id, presence: true
  # presence これは存在するか
  # presenceメソッドはオブジェクトが存在すればそのオブジェクトを返し、
  #   オブジェクトが存在しなければnilを返すメソッドとなります
  validates :content, presence: true, length: { maximum: 140 }
  # 最大140文字まで
end

####ユーザーがマイクロポストを複数所有する(has_many)関連付け
app/models/user.rb

class User < ApplicationRecord
  has_many :microposts
  # 他のモデルとの間に「1対多」のつながり
  # has_many関連付けが使われている場合、
  #   「反対側」のモデルでは多くの場合belongs_toが使われます。
.
.
.
end
```
####慣習的に正しくマイクロポストを作成する 
test/models/micropost_test.rb

```rb
require 'test_helper'

class MicropostTest < ActiveSupport::TestCase

  def setup
    @user = users(:michael)
    # このコードは慣習的に正しくない
    @micropost = @user.microposts.build(content: "Lorem ipsum") 
    # contentが"Lorem ipsum"の新しいMicropostオブジェクトを返す
  end
.
.
.
end
```
#####テスト
```
ubuntu:~/environment/sample_app (user-microposts) $ rails t
Running via Spring preloader in process 7077
Started with run options --seed 64842

  51/51: [============================] 100% Time: 00:00:04, Time: 00:00:04

Finished in 4.85194s
51 tests, 219 assertions, 0 failures, 0 errors, 0 skips
```

###演習
1.
データベースにいる最初のユーザーを変数userに代入してください。そのuserオブジェクトを使ってmicropost = user.microposts.create(content: "Lorem ipsum")を実行すると、どのような結果が得られるでしょうか?

```rb
>> user = User.first
   (1.8ms)  SELECT sqlite_version(*)
  User Load (1.1ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2021-10-17 05:28:48", updated_at: "2021-10-20 14:25:39", password_digest: [FILTERED], remember_digest: nil, admin: true, activation_digest: "$2a$12$x4xVHmtrDFjVPFFWav31UebLY8r9sdT8ImzSXGSrnqt...", activated: true, activated_at: "2021-10-17 05:28:48", reset_digest: "$2a$12$FhfOA3Y7VPJ3TJJ5nNUEkuSH0hkGrrW15uyYE9EwbrC...", reset_sent_at: "2021-10-20 14:25:39">
>> micropost = user.microposts.create(content: "Lorem ipsum")
   (0.1ms)  begin transaction
  Micropost Create (11.7ms)  INSERT INTO "microposts" ("content", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["content", "Lorem ipsum"], ["user_id", 1], ["created_at", "2021-10-22 13:53:49.381447"], ["updated_at", "2021-10-22 13:53:49.381447"]]
   (11.4ms)  commit transaction
=> #<Micropost id: 2, content: "Lorem ipsum", user_id: 1, created_at: "2021-10-22 13:53:49", updated_at: "2021-10-22 13:53:49">
```
それがあったらしい。

2.
先ほどの演習課題で、データベース上に新しいマイクロポストが追加されたはずです。user.microposts.find(micropost.id)を実行して、本当に追加されたのかを確かめてみましょう。また、先ほど実行したmicropost.idの部分をmicropostに変更すると、結果はどうなるでしょうか?

```rb
>> user.microposts.find(micropost.id)
  Micropost Load (0.3ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? AND "microposts"."id" = ? LIMIT ?  [["user_id", 1], ["id", 2], ["LIMIT", 1]]
=> #<Micropost id: 2, content: "Lorem ipsum", user_id: 1, created_at: "2021-10-22 13:53:49", updated_at: "2021-10-22 13:53:49">
>> user.microposts.find(micropost)
Traceback (most recent call last):
        1: from (irb):6
ArgumentError (You are passing an instance of ActiveRecord::Base to `find`. Please pass the id of the object by calling `.id`.)
```

3.
user == micropost.userを実行した結果はどうなるでしょうか? また、user.microposts.first == micropost を実行した結果はどうなるでしょうか? それぞれ確認してみてください。

```rb
>> user == micropost.user
=> true
>> user.micropost.first == micropost
Traceback (most recent call last):
        1: from (irb):16
NoMethodError (undefined method `micropost' for #<User:0x00007f4c14a20f28>)
Did you mean?  microposts
               microposts=
```

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?