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
model
# 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)
end
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)}
end
Currently, validation is not set on the Content side.
Seed data has already been created.
rails c
If 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)
user.contents
(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-5.2.4.4/lib/active_record/relation/delegation.rb:125:in `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
Related articles
- ruby on rails runtime error cannot be resolved
- ruby on rails - syntax error cannot be resolved
- ruby on rails - cannot start unicorn cannot load `railsconfigactive_storageservice`
- ruby on rails - cannot save nested child model
- ruby on rails 5 - actioncontroller :: invalidauthenticitytoken cannot be resolved
- ruby on rails - image cannot be saved (activestorage installed) null in image of table
- ruby on rails - you cannot change the keychain after downloading the pem file when creating an ec2 instance the matter that come
- ruby on rails 5 - rails cannot retrieve params data
- ruby - heroku run rails db: cannot migrate
- ruby on rails - webpacker cannot be compiled
- ruby on rails - the image cannot be saved in s3 and cannot be displayed in the view aws :: sigv4 :: errors :: missingcredentials
- ruby - rails cannot be started in the production environment when building an ec2 environment
- ruby on rails - [docker] rails s -b 0000 => cannot render console from 1722801!
- ruby on rails - about matters that cannot be migrated
- ruby on rails 6 - rails6 does not refer to the intended table when cutting the subdirectory under app/model
- ruby - how nice! function routes do not pass well (relation) [rails]
- ruby on rails - data cannot be saved in the intermediate table (parameters cannot be passed)
- ruby - [rails] the cart function cannot get the cart id associated with the user
- ruby - the error in rails g migration cannot be resolved
- ruby on rails - vuejs cannot find element: app error
- ruby - i want to get records that match multiple foreign keys from within an intermediate table
- ruby - about how to save db with rails devise
- ruby - when i try to save data from rails console, id, created_at, updated_at become nil
- how to write in ruby multiple loop controller
- ruby - output the total integer value held by each user
- ruby - i want to use two rails renders
- ruby - i can't get a payjp token on aws
- html - i want it to be rendered if the search word is incorrect
- ruby - about viewport introduction
- ruby - [rails] about the error that occurs when deleting likes (routing error)
As per the error message
user
IsUser
Is not..where
The result isrelationIt has become.If you want to get only one model
user = User.find (1)
Oruser = User.find_by (id: 1)
Let's do it like this.