I want to realize the process of displaying a list of contents on the client screen by referring to the relation destination table.

ER diagram


# user.rb
class User</pre>
<pre><code data-language = "rb"># content.rb
class Content</pre>
<p><br />
<strong>test data</strong></p>
<pre><code data-language = "rb"># seed.rb
require "securerandom"
User.create! (username:'test', email:'[email protected]', password:'password', confirmed_at: Time.now)
99.times do | n |
  username = SecureRandom.urlsafe_base64 (10)
  email = "example-# {n + 1}@plathoth.com"
  password = "password"
  User.create! (username: username, username,
               email: email,
               password: password,
               confirmed_at: Time.now)
users = User.order (: created_at) .take (6)
50.times do | n |
  title = "test image # {n + 1}"
  summary = Faker :: Lorem.sentence (5)
  view = rand (10000)
  media = open ("# {Rails.root} /db/fixtures/00.jpg")users.each {| user | user.contents.create! (Title: title,
                                            summary: summary,
                                            view: view,
                                            media: media)}

Currently, validation is not set on the Content side.

Seed data has already been created.

rails cIf i create a new user with and then refer to content, you can safely refer to content.

[1] pry (main)>user = User.create (username:'admin', email:'[email protected]', password:'password', confirmed_at: Time.now)
   (0.5ms) SAVEPOINT active_record_1
  User Exists (0.5ms) SELECT 1 AS one FROM `users` WHERE` users` .`email` = BINARY'[email protected]' LIMIT 1
  User Create (0.8ms) INSERT INTO `users` (`email`, `encrypted_password`,` confirmed_at`, `created_at`,` updated_at`, `username`) VALUES ('[email protected]','$2a $12 $319lUAPEZprnQH3Wm0NSpuN7quUfXkJ1HW.mMDCXQWS5lapUGpX9W', '2020-09-28 05:19:46', '2020-09-28 05:19:46', '2020-09-28 05:19:46','admin')
   (0.4ms) RELEASE SAVEPOINT active_record_1
=>#<User id: 104, email: "[email protected]", created_at: "2020-09-28 05:19:46", updated_at: "2020-09-28 05:19:46", provider : nil, uid: nil, username: "admin">
[2] pry (main)>user.contents
=>Content Load (0.6ms) SELECT `contents`. * FROM` contents` WHERE `contents`.`user_id` = 101

[3] pry (main)>user.contents.create (title: "aaaaaa", summary: "bbbbbbb", view: 11111112)
   (0.6ms) SAVEPOINT active_record_1
  Content Create (0.6ms) INSERT INTO `contents` (`title`, `summary`,` view`, `user_id`,` created_at`, `updated_at`) VALUES ('aaaaaa','bbbbbbb', 11111112, 101, '2020-09-28 03:49:54', '2020-09-28 03:49:54')
   (0.3ms) RELEASE SAVEPOINT active_record_1
=>#<Content: 0x000055ddca79fcb8
 id: 301,
 title: "aaaaaa",
 summary: "bbbbbbb",
 view: 11111112,
 media: nil,
 user_id: 101,
 created_at: Mon, 28 Sep 2020 12:49:54 JST +09: 00,updated_at: Mon, 28 Sep 2020 12:49:54 JST +09: 00>
[4] pry (main)>user.contents.where (user_id: 101)
=>Content Load (1.2ms) SELECT `contents`. * FROM` contents` WHERE `contents`.`user_id` = 101 AND `contents` .`user_id` = 101
[#<Content: 0x000055ddcab77930
  id: 301,
  title: "aaaaaa",
  summary: "bbbbbbb",
  view: 11111112,
  media: nil,
  user_id: 101,
  created_at: Mon, 28 Sep 2020 12:49:54 JST +09: 00,
  updated_at: Mon, 28 Sep 2020 12:49:54 JST +09: 00>]

From this, the relationship is considered to be working.

But if you try to do the same with test data

[1] pry (main)>user = User.where (id: 1)
=>User Load (0.6ms) SELECT `users`. * FROM` users` WHERE `users`.` id` = 1
[#<User id: 1, email: "[email protected]", created_at: "2020-09-27 11:30:09", updated_at: "2020-09-27 11:30:09", provider: nil, uid: nil, username: "hadson">]

[2] pry (main)>user.contents
NoMethodError: undefined method `contents' for #<User :: ActiveRecord_Relation: 0x000055f5bd252f98>from /usr/local/bundle/gems/activerecord- `method_missing'

Will be returned if contents do not exist.
Why does the content get a NoMethodError in the latter, even though it does almost the same thing? If the test data were not associated, an error would occur at the time of creating the seed and the test data would not be created. I felt that it was even more strange that the test data was created because I should be able to refer to the contents.

I would be grateful if anyone could teach me.

Supplementary information (FW/tool version, etc.)

Mac Catalina 10.15.6
Rails 5.2.3
ruby 2.6.3p62
mysql: 5.7
nginx: 1.16

  • Answer # 1

    Why does contents get NoMethodError in the latter?

    As per the error messageuserIsUserIs not..whereThe result isrelationIt has become.

    If you want to get only one modeluser = User.find (1)Oruser = User.find_by (id: 1)Let's do it like this.